Share this:

Today, We are going to learn How to Create Custom Helper in CodeIgniter (2022). CodeIgniter Custom Helper file is a collection of function to do task.

What is CodeIgniter?

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

Also Read : Create CRUD Application with Laravel and Vue.js

What is Helper in CodeIgniter?

CodeIgniter has more than 20 system helpers. All system helpers are stored in system/helpers directory.

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category. There are URL Helpers, that assist in creating links, there are Form Helpers that help you create form elements, Text Helpers perform various text formatting routines, Cookie Helpers set and read cookies, File Helpers help you deal with files, etc.

Check the CodeIgniter Helper Function Document for more information:

What is Helper Function in CodeIgniter?
What is Helper Function in CodeIgniter?

What we going to learn?

In this tutorial we are going to discuss and learn about CodeIgniter custom helper. You are going to learn how to create you own custom helper function as you need.

We are going to use this function in controller and view file also. We have already created the users table into database, where all the user data have stored. So Now, We will create a function to fetch a particular user details in our custom helper file.

At first we will create custom_helper.php file in application/helpers directory. The filename always would have a _helper suffix. Now create get_user_details() function, this function takes user ID argument and return the respective user details.

We will use the get_instance() function for access CodeIgniter’s native resources. get_instance() function returns the main CodeIgniter object. We have assign it into the $ci variable and it will help to using CodeIgniter database functions.

Also Read : Drag & Drop Reorder Images in PHP (2022)

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

if ( ! function_exists('get_user_details')){
   function get_user_details($user_id){
       //get main CodeIgniter object
       $ci =& get_instance();
       //load databse library
       //get data from database
       $query = $ci->db->get_where('users',array('id'=>$id));
       if($query->num_rows() > 0){
           $result = $query->row_array();
           return $result;
           return false;

We will use get_user_details() custom function in the controller and view. You should load the helper file first for use helper functions. Where “custom” is file name of the helper name, without the .php extension and the “_helper” suffix.

//load custom helper 

Now we are able to use helper function in controller and views. If we pass user ID into this function, it will be returned the respective user details.

$user_details = get_user_details(1);


Today we had learned how to create custom helper in CodeIgniter (2022). If you have any problem with the code. Please feel free to contact us by the following comment section.

Also Read : Integration PayPal Payment Gateway in PHP

Share this:

Categorized in: