Muhammad Usman

Crazy tech lover, endless learner
Home

WordPress like Option feature for your CodeIgniter application

If you have developed a large application you may have faced this problem, in some cases we have some settings or options to save in the database but the data is not so large to create a db table for it, also it may do not require many rows. In that case we should have a table for settings, where we can store all our settings or options with a key then retrieve it. Our option can be a text value or an array or an object. WordPress has a really good feature for such cases its the Option Mechanism. By using the appropriate function, options can be added, changed, removed, and retrieved.

I loved this feature too much that I added such feature in my CodeIgniter applications, because I don’t always use WordPress. Now I want to share my code with you which may help you in various tasks. Its not the exact same as WordPress. I have used 4 functions for it add_option, update_option, get_option and delete_option. The good part is you can store a PHP array, object or a string value (including number) with add_option('name_of_option','value') or with update_option('name_of_option','value') and whenever you retrieve your data with get_option('name_of_option') it returns the data with the same format.
(Sorry, if you have used it in WordPress you may already know it and some info below, but its for those who haven’t used).

You may download the code from GitHub, also any contribution is highly appreciated.

Functions:

add_option('name_of_option','value') will store data in the db with name_of_option if the name_of_option is already used it will return false, use add_option if you want to store a data for the first time such as installing an app.

update_option('name_of_option','value') will update the name_of_option if it is already used. If name_of_option is not found it will add name_of_option. So use update_option in general case.

get_option('name_of_option') will return the value of name_of_option in original data type. Will return false if option is not found.

delete_option('name_of_option') will delete the option and return true/false on success/failure.

Usage Example:

Below is the usage for CodeIgniter controller, I have made it as a CodeIgniter Helper. You will find instructions below on how to implement this Helper. I assume you have database set up in application/config/database.php

public function index()  
{  
    //load our helper,  
    //better to autoload it by editing application/config/autoload.php  
    $this->load->helper('option_helper');
    
    //text value example  
    update_option('username','Usman');  
    echo get_option('username');  
    //array example  
    $user_info=array(  
        'username' => 'Usman',  
        'profession' => 'Developer',  
        'location' => 'Sylhet, Bangladesh',  
    );  
    update_option('user_info',$user_info);  
    $return_value=get_option('user_info');  
    print_r($return_value);  
    echo $return_value['location'];
    
    //delete example
    
    delete_option('username');  
    //delete_option('user_info');  
}  

Implementation:

1. Create a table in your database like below (of course its MySQL) :

 

CREATE TABLE IF NOT EXISTS `tbl_option` (

    `option_id` bigint(20) NOT NULL AUTO_INCREMENT,
    
    `option_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
    
    `option_value` longtext COLLATE utf8_unicode_ci NOT NULL,
    
    `option_type` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
    
    PRIMARY KEY (`option_id`),
    
    UNIQUE KEY `option_name` (`option_name`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=59 ;

2. As I have made a CodeIgniter Helper for this, keep the code in application/helpers folder (in most cases) and name it option_helper.php. I haven’t used any model for simplicity.

3. Copy the code below and paste it to your option_helper.php, that’s it.

 

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
function add_option($name,$value)  
{  
    $CI =& get_instance();  
    $CI->load->database();  
    $query=$CI->db->select('*')->from('tbl_option')->where('option_name',$name)->get();
    
    //option already exists  
    if($query->num_rows() > 0)  
    return false;
    
    $data_type='text';  
    if(is_array($value))  
    {  
        $data_type='array';  
        $value=serialize($value);  
    }  
    elseif(is_object($value))  
    {  
        $data_type='object';  
        $value=serialize($value);  
    }
    
    $data=array(  
        'option_name'=>$name,  
        'option_value'=>$value,  
        'option_type'=>$data_type,  
    );  
    $CI->db->insert('tbl_option',$data);  
}

function update_option($name,$value)  
{  
    $CI =& get_instance();  
    $CI->load->database();

    $data_type='text';  
    if(is_array($value))  
    {  
        $data_type='array';  
        $value=serialize($value);  
    }  
    elseif(is_object($value))  
    {  
        $data_type='object';  
        $value=serialize($value);  
    }

    $data=array(  
        'option_name'=>$name,  
        'option_value'=>$value,  
        'option_type'=>$data_type,  
    );  
    $query=$CI->db->select('*')->from('tbl_option')->where('option_name',$name)->get();
    
    //if option already exists then update else insert new  
    if($query->num_rows() < 1) return $CI->db->insert('tbl_option',$data);  
    else return $CI->db->update('tbl_option',$data,array('option_name'=>$name));  
}

function get_option($name)  
{  
    $CI =& get_instance();  
    $CI->load->database();  
    $query=$CI->db->select('*')->from('tbl_option')->where('option_name',$name)->get();  
    //option not found  
    if($query->num_rows() < 1) return false; $option=$query->row();
    
    if('text'==$option->option_type)  
        $value=$option->option_value;  
    elseif('array'==$option->option_type || 'object'==$option->option_type)  
        $value=unserialize($option->option_value);
    
    return $value;  
}

function delete_option($name)  
{  
    $CI =& get_instance();  
    $CI->load->database();  
    return $CI->db->delete('tbl_option',array('option_name'=>$name));  
}  

If you have found it useful, please comment below. Any bug report or suggestions are most welcome.