Category Archives: Codeigniter Tutorial
Directory Helper
Directory Helper : Codeigniter provides this helper(directory) which is used to work with directories. Here in this tutorial we are going to explain this helper.
Directory Helper
Load Helper
You can load directory helper simply as below β
Codeigniter Directory Helper Example:
$this->load->helper('directory'); |
Functions
Following functions are available in this helper β
directory_map Function
directory_map Function Syntax:
directory_map($source_dir, $directory_depth = 0, $hidden = FALSE); |
Parameters:
- $source_dir (string) β Source directory Path
- $directory_depth (int) β Depth of directories for traversing (0 = fully recursive, 1 = current dir, etc)
- $hidden (bool) β Whether to include hidden directories or not.
- Returns:Array of files
- Return type:array
The above functions gives the array of files in a directory.
Date Helper
Codeigniter Date Helper : Codeigniter provides the helper which is used to work with dates. Here in this tutorial we are going to explain how you can use date helper in Codeigniter. You can use our online editor run and see the output of the example.
Date Helper in Codeigniter
Let us go step by step β
Load Date Helper
First of all load helper(data) before using it. You can the helper(date) simply as below-
Codeigniter Date Helper: Example
$this->load->helper('date'); |
After Loading this helper now you are able to use itβs functions.
Functions of Date Helper
Following functions are present in the date helper.
now Function
This function returns the current time as a UNIX timestamp, referenced by the serverβs local time or any other PHP suported timezone based upon the time reference setting in config.
Codeigniter now function syntax
now($timezone = NULL) |
Parameters:
- $timezone (string) β Timezone
- Returns (int) : UNIX timestamp
Now Function Example :
If $timeZone = βAmerica/Costa_Ricaβ; is not passed it will give the time() based on the time_reference settings.
2. mdate Function
This function is somehow similar to the date() function. In addition it enables you to use this function in mysql style date function.
Codeigniter mdate function syntax
mdate($datestr = '', $time = '') |
Parameters:
- $datestr (string) β Date String
- $time (int) β Unix Timestamp
- Returns (int) : String.
mdate Function Example :
Codeigniter mdate function syntax
$datestr = ' %Y - %m - %d - %h:%i %a'; $time = time(); echo mdate($datestr, $time); |
More Functions
Letβs have look over more functions and example.
3. standard_date Function
Codeigniter standard_date function syntax
standard_date($format = 'DATE_RFC822', $time = NULL) |
Parameters:
- $format (string) β Date Format
- $time (int) β Unix Timestamp
- Returns (int) : String.
standard_date Function Example :
Codeigniter standard_date function syntax
$datestr = ' %Y - %m - %d - %h:%i %a'; $time = time(); echo mdate($datestr, $time); |
4. local_to_gmt Function
This function converts the UNIX timestamp into GMT.
Codeigniter local_to_gmt function syntax
local_to_gmt($time = NULL) |
Parameters:
- Returns (int) : Int.
$time (int) β Unix Timestamp
Example :
5. gmt_to_local Function
This function converts GMT timestamp into the localized UNIX timestamp.
Codeigniter gmt_to_local function syntax
gmt_to_local($time = NULL) |
Parameters:
- Returns (int) : Int.
$time (int) β Unix Timestamp
6. mysql_to_unix Function
This function converts mysql timestamp into the UNIX timestamp.
Codeigniter mysql_to_unix function syntax
mysql_to_unix($time = NULL) |
Parameters:
- Returns (int) : Int.
$time (int) β Unix Timestamp
7. unix_to_human Function
This function converts UNIX timestamp to human date format.
Codeigniter unix_to_human function syntax
unix_to_human($time = '', $seconds = FALSE, $format = 'us') |
Parameters:
- $time (int) β Unix Timestamp
- $seconds (bool) β Pass true to show seconds else false.
- $format (string) β String Format.
- Returns (String) : String.
8. human_to_unix Function
This function date into the UNIX timestamp.
Codeigniter human_to_unix function syntax
human_to_unix($dateString) |
Parameters:
- $dateString β Date String
- Returns (Int) : int.
9. nice_date Function
This function takes bad date string and converts it into the proper formatted date..
Codeigniter nice_date function syntax
nice_date($badDate, $format = FALSE) |
Parameters:
- $badDate β Date String not in proper format.
- $format β Format To Be returned.
- Returns (Date) : formatted date.
Example
$badDate = '199005'; $date = nice_date($badDate, 'Y-m-d'); // Will give: 1990-05-01
10. timespan Function
This function formats the UNIX timestamp date..
Codeigniter timespan function syntax
timespan($seconds = 1, $time = '', $units = '') |
Parameters:
- $seconds β No Of seconds.
- $time β UNIX timestamp.
- $units β time unit to return.
- Returns (Date) : formatted date.
11. days_in_month Function
This function returns the number of days in specified year.
Codeigniter days_in_month function syntax
days_in_month($month = 0, $year = '') |
Parameters:
- $month β Month.
- $year β Year.
- Returns (Days) : No of days.
Example
echo days_in_month(08, 2006);
12. date_range Function
This function returns the array of dates between two specified dates.
Codeigniter date_range function syntax
date_range($unix_start = '', $mixed = '', $is_unix = TRUE, $format = 'Y-m-d') |
Parameters:
- $unix_start β UNIX timestamp start date.
- $mixed β UNIX timestamp of the range end date.
- $is_unix β This if set to FALSE if $mixed is not a timestamp.
- $format β Output format.
- Returns (Dates) : Returns all dates within the range.
Example
$dateRange = date_range('2014-03-01', '2014-01-20'); print_r($dateRange); // will give you all dates between the range.
13. timezones Function
This function returns the returns the number of hours offset from UTC using the timestamp.
Codeigniter timezones function syntax
timezones($timezone) |
Parameters:
- $timezone β Numeric timezone.
- Returns (int) : Returns hours diff.
14. timezone_menu Function
This function is used to create the dropdown menu of timezones.
Codeigniter timezone_menu function syntax
timezone_menu($default = 'UTC', $class = '', $name = 'timezones', $attributes = '') |
Parameters:
- $default (string) β Timezone
- $class (string) β Class name
- $name (string) β Menu name
- $attributes (mixed) β HTML attributes
- Returns (HTML) : Returns HTML Dropdown menu of timezones.
Cookie Helper
Codeigniter Cookie Helper contains the functions that are used in working with cookies. Here in this tutorial we are going to explain how you can create cookies using this helper. Let us go step by step to learn about the Cookie Helper.
Cookie Helper in Codeigniter Example
Load This Helper
Before using the helper function you must load this helper as below β
Cookie Helper in Codeigniter Example:
$this->load->helper('cookie'); |
After Loading this function you can use itβs functions.
Cookie Helper Functions
Let us go through the available functions in this helper.
set_cookie Function
Syntax of set_cookie function
Codeigniter set_cookie function syntax:
set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE); |
Parameters:
- $name (mixed) β Name of Cookie.
- $value (string) β Cookie value.
- $expire (int) β Number of seconds until expiration.
- $domain (string) β Cookie domain ex.- www.tutorialsplane.com.
- $path (string) β Cookie path.
- $prefix (string) β Cookie name prefix.
- $secure (bool) β To send the cookie using HTTPS.
- $httponly (bool) β To hide the cookie from JavaScript.
get_cookie Function
Syntax of get_cookie function
Codeigniter get_cookie function syntax:
get_cookie($index, $xss_clean = NULL); |
Parameters:
- $index (string) β Name of Cookie.
- $xss_clean (bool) β If you want to apply xss filtering to returned value set true else it will be false by default.
Returns:
Returns cookie value. If value not found it will return NULL.
delete_cookie Function
Syntax of delete_cookie function
Codeigniter delete_cookie function syntax:
delete_cookie($index, $domain, $path, $prefix); |
Parameters:
- $name (mixed) β Name of Cookie.
- $domain (string) β Cookie domain ex.- www.tutorialsplane.com.
- $path (string) β Cookie path.
- $prefix (string) β Cookie name prefix.
Returns:
Returns void.
delete_cookie Example
If you do not want to specify other params you can delete the cookies as below β
Codeigniter delete_cookie Example:
delete_cookie('my_cookie'); //my_cookie name of the cookies |
Where my_cookie is name of the cookie.
Examples
Letβs have look over examples on set, get and delete cookie.
Codeigniter Set Cookie Example
You can set cookie value simply as below β
Codeigniter Set Cookie Example :
public function setMyCookie(){ $this->load->helper('cookie'); $cookie = array( 'name' => 'username', 'value' => 'test@tutorialsplane.com', 'expire' => time()+86500, 'domain' => 'www.tutorialsplane.com', // You can add .localhost for local machine 'path' => '/', 'prefix' => 'tutorialsplane_', ); set_cookie($cookie); } |
Codeigniter Get Cookie Example
You can get cookie value simply as below β
Codeigniter Get Cookie Example :
public function getMyCookie(){ $this->load->helper('cookie'); //get_cookie('prefix_name'); $cookieVal = get_cookie('tutorialsplane_username'); echo $cookieVal; } |
Codeigniter Delete Cookie Example
You can delete cookie value simply as below β
Codeigniter delete Cookie Example :
public function deleteMyCookie(){ $this->load->helper('cookie'); // delete cookie example $cookie = array( 'name' => 'username', 'value' => '', 'expire' => '0', 'domain' => 'www.tutorialsplane.com', // You can add .localhost for local machine 'prefix' => 'tutorialsplane_' ); delete_cookie($cookie); } |
The above example will delete the cookie set in the above example.
CAPTCHA Helper
CAPTCHA Helper : Codeigniter provides the CAPTCHA Helper which contains the functions that are used to create CAPTCHA images. Here is in this tutorial we are going to explain how you can create CAPTCHA images using the codeiniterβs CAPTCHA. CAPTCHA is used for human verification. Basically it is used to prevent the automated robots, bots, spams to access the website. CAPTCHA is basically image which contains some random string, numbers which needs to be verified by the user.
CAPTCHA Helper in Codeigniter
Let us go step by step to understand how captcha helper works in codeigniter.
Load CAPTCH Helper
You can load CAPTCHA(helper) simply as below β
Load CAPTCHA Helper in Codeigniter:
$this->load->helper('captcha'); |
Now you can user CAPTCHA(helper) functions to create CAPTCHA images.
How to use Codeigniter CAPTCHA Helper ?
If CAPTCHA(Helper) is loaded you can create CAPTHCA simply as below β
Load CAPTCH(Helper) in Codeigniter:
load->helper('captcha'); $val = array( 'word' => rand(1000, 10000), 'img_path' => './captcha/', 'img_url' => 'http://tutorialsplane.com/runtest/codeigniter/demo/captcha/', 'font_path' => 'arial.ttf', 'img_width' => '200', 'img_height' => 50, 'expiration' => 7200, 'word_length' => 7, 'font_size' => 20, 'img_id' => 'Imageid', 'pool' => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'colors' => array( 'background' => array(255, 255, 255), 'border' => array(255, 255, 255), 'text' => array(0, 0, 0), 'grid' => array(255, 40, 40) ) // background : White // border: White //Text: black text and red grid ); $captcha = create_captcha($val); echo $captcha['image']; } } ?> |
In the above example we have created a controller and added a method which handles the captcha generation.
If you run the above example it will produce output something like this β
More About CAPTCHA Helper
Letβs have look over more example and demo here.
Array Helper
Array Helper β Codeigniter Array Helper file contains the functions which are used for arrays. You can load this helper and use itβs functions. Here in this tutorial we are going to explain how you can load array helper in codeigniter and use itβs functions. We will also explain how you can load helper on view file and use itβs functions. You can use our online editor to run and see the output of the example.
Array Helper in Codeigniter
Let us go step by step to understand the codeigniter array helper.
Load Array Helper in Codeigniter
You can load the array helper using the following syntax β
Load Array Helper in Codeigniter Example :
$this->load->helper('array'); |
Let us go through the available
Array Helper Functions
There are following functions available in the codeigniter Helper.
Element
Syntax :
element($item, $array[, $default = NULL]) |
Parameters β
- $item β Item to fetch from array.
- $array β Input array.
- $default β Default value to be returned if array is not valid.
Returns β
Returns NULL on failure. Returns item on success.
This function is used to fetch an item from array. This functions checks the index and its value. If the index value exists it returns the value else it returns NULL or the specified value in third parameter $default.
Example β
Codeigniter Array Helper function β element: Example
901, 'sku' => 'red-bounce', 'color' => 'gray', 'price' => '' ); echo element('id', $array); // will return 901 echo element('price', $array); // return null echo element('price', $array, '$99'); // return $99 ?>` |
Elements
Syntax :
elements($items, $array[, $default = NULL]) |
Parameters β
- $items β Array of Items to fetch from array.
- $array β Input array.
- $default β Default value to be returned if array is not valid.
Returns β
Returns NULL on failure. Returns items on success.
This function is used to fetch multiple items from an array. This functions checks the index and value of each array item is set or not. If the index value exists it returns the value else it returns NULL or the specified value in third parameter $default.
Example β
Codeigniter Array Helper function β element: Example
901, 'sku' => 'red-bounce', 'color' => 'gray', 'price' => '' ); echo elements(array('id','sku'), $array); // will return array('id'=>901, 'sku'=>'red-bounce'); echo elements(array('id','sku', 'price'), $array); // will return array('id'=>901, 'sku'=>'red-bounce', 'price'=>NUll); echo elements('price', $array, '$99'); // return array('id'=>901, 'sku'=>'red-bounce', 'price'=>$99); ?>` |
Random Element
This returns the random element from the specified array.
Syntax :
random_element($array) |
Where $array is input array.
Example
Syntax :
$array = array(1, 100, 21, 34, 589, 90, 192); echo random_element($array); // will give any random number |
Codeigniter libraries
Codeigniter libraries : Codeigniter libraries provides you rich functionality with ready made libraries. You can also create your own library.
Using Codeigniter libraries
$this->load->library('library_name');
For Example If We Need Form validation library
$this->load->library('form_validation');
Load Multiple Codeigniter libraries
$this->load->library(array('form_validation','email'));
You Can Also Auto-load Libraries
Go to application/config/autoload.php
Now go to the below lines and add the library name you want to automatically load.
$autoload['libraries'] = array('email','form_validation');
Codeigniter Load Helper
Codeigniter Load Helper : There are two options to load helpers in codeigniter either load inline or autoload it before using it.
Syntax : Codeigniter Load Helper
$this->load->helper('name');
Example
Load Url Helper.
$this->load->helper('url');
Codeigniter Load Multiple Helper
$this->load->helper(array('name1','name2','name3'));
Example
Load Url, form Helper.
$this->load->helper('url','form');
Auto-load Codeigniter Helper
Go to application/config/autoload.php
Go to Below Line
$autoload['helper'] = array('helper1','helper2');
Example
Load url, form Helper.
$autoload['helper'] = array('url', 'form');
How To Use
Once you have loaded the helper in the controller now you can use helper functions in view for example :
Which Will Generate a link.
Codeigniter Helpers Example
Codeigniter Helpers : Codeigniter Helpers are collection of functions which provides you help in your task.
Codeigniter Helpers are written in procedural language. Each functions in helper are independent of other.
Before Using the codeigniter functions you need to load them. You can autoload helpers which will be available
globally.
Some Codeigniter Helpers Example Are As
Url Helper, Cookie helper, Form Helper etc.
$this->load->helper('url');
Codeigniter Connect Database
Codeigniter Connect Database : You can connect to database in codeigniter as below.
Steps : Codeigniter Connect Database
Go to the application/config/databse.php
Step 1
Go to the below lines:
$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'username', 'password' => 'password', 'database' => 'your_databse_name', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => TRUE, 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE );
Add the above required parameter β
Hostname, Database Username and Password, Database name and other options if you need.
Step 2
Go to application/config/autoload.php
Go on the line :
$autoload['libraries'] = array();
Now Add
$autoload['libraries'] = array('database');