Online Snippets Library

Create own helper In Codeigniter

Codeigniter Helpers,as the name suggests help us to do task.CodeIgniter helper file is simply a collection of functions of particular category.CodeIgniter Comes with more than 20 system helpers.

CodeIgniter first search helper in your application/helpers directory.If directory does not have specified helper than CodeIgniter look in your global system/helpers folder.


Create In Codeigniter

We will use following step to create own helper In Codeigniter.

Step 1

At first we will create my_helper.php file in application/helpers directory.The filename always would have a _helper suffix.

We will use the get_instance() function for access CodeIgniter's default resources.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('get_name')){
   function get_name($id){
       //get main CodeIgniter object
       $ciHelper =& get_instance();
       //load databse library
       //get data from database
       $query = $ciHelper->db->get_where('mainCategory',array('id'=>$id));
       if($query->num_rows() > 0){
           $result = $query->row_array();
                   return $result;
           return false;

Load helper In Codeigniter

Go to "application/cofig/autoload" and search "$autoload['helper']", Pass your helper file name in array like below. $autoload['helper'] = array('my_helper');

Use Helper Codeigniter

If we pass id into this function, it will be returned the respective Category details.

$result = get_name('1');