Tag Archives: codeigniter tutorials

Codeigniter url segments

Codeigniter request uri

Codeigniter Uri Segments Can be Retrived by using the


$this->uri->segment(n);

For Example

www.demoexample.com/user/register/66

$this->uri->segment(1); – Will Return Controller name “user” from the url.

$this->uri->segment(2); – Will Return Method name (ie. Action) “register” from the url.

$this->uri->segment(3); – Will Return Parameter name (ie. Segment) “66” from the url which is recived in method

register.

codeigniter cache example

codeigniter cache example

Enable Cache by putting the following code in any controller :


$this->output->cache(n);

Where n is number of minutes
Note :
Make Sure “application/cache” folder is writable.

Full Example :

function your_controller()
{
       $this->output->cache(2); //cache page for 2 minute
       $this->load->view('your_view'); 
}

You can also use the data base caching for improving the performance of the system.

Please do not use the database before understanding it to avoid the real time data and cache data issues.

codeigniter forms

codeigniter forms

First Load Helper :

$this->load->helper('form');
$action = "users/create";
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open($action,$attributes);

echo form_close();

or for multipart you can use :


echo form_open_multipart($action,$attributes);

Codeigniter Form Fields :

form_input :

$data = array(
              'name'        => 'name',
              'id'          => 'name',
              'value'       => 'Name',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_input($data);

form_password() :

$data = array(
              'name'        => 'password',
              'id'          => 'password',
              'value'       => 'Password',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_password($data);

Complete Code Together :


$action = "users/create";
$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open($action,$attributes);



$data = array(
              'name'        => 'name',
              'id'          => 'name',
              'value'       => 'Name',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_input($data);

$data = array(
              'name'        => 'password',
              'id'          => 'password',
              'value'       => 'Password',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
            );

echo form_password($data);

echo form_submit('mysubmit', 'Submit');

echo form_close();

Codeigniter Load Model | Unable to load Model

Codeigniter Load Model | Unable to load Model

Suppose The model name is User_model

///model class in Model folder


class User_model extends CI_Model {
  public function Update_users(){
    //some query to update 
  }
}

suppose we want to load and call method Update_users() from controller

First of all load model as :


$this->load->model("User_model");

now if you want to call the Update_users() method :


$thi->User_model->Update_users();

Codeigniter Helpers

Codeigniter Helpers are a collection of functions in a particular category which full fills the requirement for particular tasks.

Example : url helper which provides functions related to creating links .

How to load Codeigniter helpers :

$this->load->helper('name');

for loading url helper :

$this->load->helper('url');

Loading multiple helpers :

$this->load->helper( array('helper1', 'helper2', 'helper3') );

You can also autoload helpers :

to autoload helpers go to autoload.php

add the helper in autoload you want to auto load.

 

Codeigniter image thumbnail example

Codeigniter image thumbnail example

Codeigniter image thumbnail library | codeigniter image thumbnail example | codeigniter image thumbnail function | codeigniter create image thumbnail
Create custom image thumbnail library in codeigniter .
1 . Go to application/library
2 . Create Thumb.php file
3 . Add the following code in Thumb.php

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Thumb{
  public  function createThumbs($pathToImages,$fname, $pathToThumbs, $thumbWidth ,$thumbHeight) 
{
  $srcFile = $pathToImages.$fname;
  $thumbFile = $pathToThumbs.$fname;
  if(!empty($fname))
  {
  $type = strtolower(substr( $fname , strrpos( $fname , '.' )+1 ));

 $thumbnail_width=$thumbWidth;
 $thumbnail_height=$thumbHeight;
 
  switch( $type ){
    case 'jpg' : case 'jpeg' :
         try{
           $src = imagecreatefromjpeg( $srcFile ); break;
       } 
       catch(Exception $e) {
          
           log_message('System Error', $e->getMessage());
       }
      ///$src = imagecreatefromjpeg( $srcFile ); break;
  case 'jpeg' : case 'jpeg' :
       try{
           $src = imagecreatefromjpeg( $srcFile ); break;
       } 
       catch(Exception $e) {
          
           log_message('System Error', $e->getMessage());
       }
    case 'png' :
        if ( ! function_exists('imagecreatefrompng'))
        {
            $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_png_not_supported'));
            return FALSE;
        }
      $src = imagecreatefrompng( $srcFile ); break;
    case 'gif' :
         if ( ! function_exists('imagecreatefromgif'))
        {
            $this->set_error(array('imglib_unsupported_imagecreate', 'imglib_gif_not_supported'));
            return FALSE;
        }
      $src = imagecreatefromgif( $srcFile ); break;
  }
     list($width_orig, $height_orig) = getimagesize($srcFile); 
     $ratio_orig = $width_orig/$height_orig;
    
    if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
       $new_height = $thumbnail_width/$ratio_orig;
       $new_width = $thumbnail_width;
    } else {
       $new_width = $thumbnail_height*$ratio_orig;
       $new_height = $thumbnail_height;
    }
    
    $x_mid = $new_width/2;  //horizontal middle
    $y_mid = $new_height/2; //vertical middle
    
    $process = imagecreatetruecolor(round($new_width), round($new_height)); 
    
    imagecopyresampled($process, $src, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
    $dest_img = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 
    imagecopyresampled($dest_img, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thu    mbnail_height, $thumbnail_width, $thumbnail_height);

    switch( $type ){
    case 'jpg' : case 'jpeg' :
      $src = imagejpeg( $dest_img , $thumbFile ); break;
    case 'png' :
      $src = imagepng( $dest_img , $thumbFile ); break;
    case 'gif' :
      $src = imagegif( $dest_img , $thumbFile ); break;
  }
  
  }    
    
}


 
}
?>

How to Use :
1 . Load Library & call methiod createThumbs()

You can make thumbs to those images which has been upploaded in folder ‘images’.
You can call this library just after your image upload is completed.

example :
suppose we have to make thumb which is uploaded in folder ‘images’ named as ‘test.jpg’
$this->load->library(‘Thumb’);

$pathToImages=’images/’;
$pathToThumbs = ‘images/thumbs/’;
$imagename = ‘test.jpg’;
$thumbWidth =’100′;
$thumbHeight = ‘100’;
$this->Thumb->createThumbs($pathToImages,$imagename, $pathToThumbs, $thumbWidth ,$thumbHeight);

$

How to enable query string in codeigniter


How to enable query string in codeigniter

If you want to use query string like this :

index.php?page_no=10&id=12&ref=home_page

Go to application/config.php

change

$config[‘enable_query_strings’] = FALSE;

to

$config[‘enable_query_strings’] = TRUE;

Now you can use query strings in codeigniter controllers.
can use this How to enable query string in codeigniter.

How to get current date time in codeigniter?

How to get current date time in Codeigniter– It is pretty simple & quick to get the current time & date in Codeigniter. First, load date helper in the controller or autoload it in the _autoload file, After loading this helper you will be able to access its methods defined for time & date.

Get current date time with Date Format in Codeigniter.

How to get current date time in codeigniter

Now let us go step by step to understand how to date helper is used. 

Load Date Helper

First Make sure the date helper is loaded, If not then load it simply as below-

$this->load->helper('date');

Get Date Time

Once the date helper is loaded you get the current date and time as –

$format = "%Y-%m-%d %h:%i %A";
echo mdate($format);
// this will print 
// 2017-09-06 08:15 PM 

Date Format

You can pass the date time format to the date($format) function where the $format is the date format. Apart from Codeigniter DateTime, we can also use native date functions to get the current DateTime – For example, you can use the now() function to get the current time & date.

Codeigniter Date Time Demo

load->helper('date');

$format = "%Y-%m-%d %h:%i %A";
echo mdate($format);

}

}
?>

Try it »

If run the above example it will produce output something like this- How to get current date time in codeigniter

For More Details About Date Helper & Functions Read – Date helper

I hope this article was helpful for you.

How to display likes in facebook style in php

How to display likes in facebook style in php

Use the following function to format facebook likes, comments count

if(!function_exists(‘format_num’)){
function format_num($n) {
$arr = array(“K”, “M”, “G”, “T”);
$out = “”;
while ($n >= 1000 && count($arr ) > 0) {
$n = $n / 1000.0;
$out = array_shift($arr );
}
return round($n, max(0, 3 – strlen((int)$n))) .” $out”;
}
}

To use this function in codeigniter add this function in

system/helper/url_helper.php file

will give like counts in facebook style.

Ex 100 likes

1000 likes = 1 K

10000 likes = 10 K