Skip to content

Instantly share code, notes, and snippets.

@hengkiardo
Created October 19, 2012 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hengkiardo/3917710 to your computer and use it in GitHub Desktop.
Save hengkiardo/3917710 to your computer and use it in GitHub Desktop.
WordPress like option feature for your CodeIgniter application
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
Opions Helper for CodeIgniter
Database Table for this Helper:
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 ;
*/
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));
}
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');
}
@usmanhalalit
Copy link

You are not allowed to copy my code without removing copyright comment https://github.com/usmanhalalit/option_helper/blob/master/option_helper.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment