Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How to create custom helpers in CodeIgniter

How to create custom helper in CodeIgniter

Posted on June 6, 2018December 17, 2022 By XpertPhp No Comments on How to create custom helper in CodeIgniter

In this article, We will discuss how to create custom helper in CodeIgniter. CodeIgniter allows the creation of a custom helper. So we can easily create a custom helper. we will take the latest example and learn how to use a custom helper in CodeIgniter. so let’s follow the below step.

Read Also:
Codeigniter CRUD Operation With Ajax Example

Create the helper file.

first, we will create a helper file in the “application/helpers/commonfunction_helper.php”. put some code in this file. here in this file. we will get the student all detail using the custom helper function.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/*
* get_all_rows
*
*/
function get_all_student_rows()
{
$CI =& get_instance();
$CI->db->select();
$query = $CI->db->get('students');
//echo $CI->db->last_query();die;
return $query->result_array(); // This will return array
}
?>

Load The Custom helper

Now, we will load the custom helper so we will open the application/config/autoload.php file and add the helper name to the helper array.

PHP
1
2
3
4
5
6
7
8
9
/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
| $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('commonfunction_helper');
See also  How to implement HMVC in codeigniter 3

How to use the custom helper function in Controller or Views.

now, we can use the custom helper function in Controller or Views. we can get the student detail in Controller or Views using the call get_all_rows helper function.

Controller Example

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Student extends CI_Controller {
    function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
      $student_detail = get_all_student_rows();
      echo "<pre>";
      print_r(student_detail);
      echo "</pre>";
    }
}

View Example

PHP
1
2
3
4
5
6
<?php
     $student_detail = get_all_student_rows();
     echo "<pre>";
     print_r(student_detail);
     echo "</pre>";
?>

So finally, I hope you like this article.

Codeigniter, MySql Tags:codeigniter helper, codeigniter tutorial

Post navigation

Previous Post: PHP Array convert into string
Next Post: CodeIgniter join query example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com