Create and use helper function within codeigniter project

In this post, We will discuss about helper function in codeigniter. Helper function is a collection of common functions, that we can use any where in the project. Purpose of use this type of functions is to avoid repeated code in controller so that a unique function could be created and changing of this function would effect to everywhere, wherever we called this function. Let see with an example.

1. First of all we are going to create a helper file inside application/helpers directory called my_helper.php.In this file we are making a time ago function. Time ago function is for that activity time, which has been done some time before .Please find below the code.

my_helper.php


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

if ( ! function_exists('timeAgo')){
function timeAgo($timeAgo)
{
$timeAgo = strtotime($timeAgo);
$curTime = time();
$timeElapsed = $curTime - $timeAgo;
$seconds = $timeElapsed;
$minutes = round($timeElapsed / 60);
$hours = round($timeElapsed / 3600);
$days = round($timeElapsed / 86400);
$weeks = round($timeElapsed / 604800);
$months = round($timeElapsed / 2600640);
$years = round($timeElapsed / 31207680);
/* Seconds&nbsp; Calculation*/
if ($seconds &lt;=60) {
return'Just Now';
} /* Minutes */
elseif ($minutes &lt;=60) {
if ($minutes ==1) {
return"One Minute ago";
} else {
return $minutes." time ago minutes";
}
} /* Hours */
elseif ($hours &lt;=24) {
if ($hours ==1) {
return"One hour ago";
} else {
return $hours." Hour Ago";
}
} /* Days */
elseif ($days &lt;=7) {
if ($days ==1) {
return"One day ago";
} else {
return $days." days ago";
}
} /* Weeks */
elseif ($weeks &lt;=4.3) {
if ($weeks ==1) {
return"One week ago";
} else {
return $weeks." week ago";
}
} /* Months */
elseif ($months &lt;=12) {
if ($months ==1) {
return"One month ago";
} else {
return $months." month ago";
}
} /* Years */
else {
if ($years ==1) {
return"One year ago";
} else {
return $years." year ago";
}
}
}
}

2. Next, Load this helper file.Like other inbuilt helper in codeigniter, we also need to load our own custom helper.  We can load it either in every controller or globally through autoload in config file.

This is how we can load helper in our controller for once.

<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Payroll extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('my_helper');
}
}
And this is how we can load helper globally.
/*
| -------------------------------------------------------------------
| Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|   $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('url','html','file','form','my_helper');

3. Finally, we can call that helper functions from controller or view. Just pass the value with the parameter of function wherever you need that function.

Thank you for reading this post. we hope you like this Post, Please feel free to comment below, your suggestion. if you face any issue with this code let us know. We’d love to help!

 

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , ,