Category Archives: Codeigniter

Codeigniter Folder Structure

Codeigniter Folder Structure is Listed as below :

There Basically two main folders application and system folder.

application folder

  • cache
  • Config
  • Controllers
  • core
  • errors
  • helpers
  • hooks
  • language
  • libraries
  • logs
  • models
  • thirdparty
  • views

system folder

  • core
  • database
  • fonts
  • helpers
  • language
  • libraries

Hook point in Codeigniter

Hook point in Codeigniter –

There are following hook points available in codeigniter:

  • post_controller_constructor
  • pre_controller
  • cache_override
  • display_override
  • post_controller
  • post_sytem
  • pre_system

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 get current url


Codeigniter get current url : Url helper is used to get the current url in Codeigniter. It contains the function that returns the current url. The function current_url() is used to get the current url in codeigniter. Using this function you can get current url anywhere on view, model, controller, Library or helper as well.


You Can Get The current url in codeigniter using the following function-

Codeigniter get current url On View | Controller | Model | Syntax Example

Codeigniter get current url

Here is syntax for current url-

current_url();

This function return current url. If want to get get current url anywhere on Controller, View or Model use the above function.

Make Sure the Url Helper is loaded before calling the function –

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

Url Helper contains the functions which assist in url working. You can load this helper in _autoload.php because this function is frequently used in codeigniter.

I hope this article was helpful for you.

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);

$