
Some people might find WordPress’s get_option and update_option function helpful over filesystem based configuration where you don’t want user to frequently update the configuration files. Today let me show you how we can utilize the same functionality in CodeIgniter using Option Library.
Requirements & Basic Informations
Before we start, let lay out some fact about Option Library:
- is based and developed on CodeIgniter 1.7.1, it might work with previous version but please do test it out first.
- use CodeIgniter build-in Database Class with ActiveRecord.
- we cache the result on every pageview, Option Library query the table and cached it in an Array which mean Option Library doesn’t overhaul your database.
Getting Started
To use the Option library, you have to checkout a copy of latest Ext.CodeIgniter or grab the following files:
- http://myci.googlecode.com/svn/trunk/system/application/libraries/Option.php
- http://myci.googlecode.com/svn/trunk/system/application/config/application.php
Database
Before you can configure Option Library let first create a new table:
CREATE TABLE `options` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 255 ) NOT NULL , `value` TEXT NOT NULL ) ENGINE = MYISAM ;
Configuration
Open system/application/config/application.php and go to line 35. You need to enable Option Library (allowing CodeIgniter the fetch from your option table) and identify the table schema based on the table you just created above.
$config['option']['enable'] = TRUE; $config['option']['table'] = 'options'; $config['option']['attribute'] = 'name'; $config['option']['value'] = 'value';
Using the Option Library
This is just a simple example how you use the library.
class Main extends Controller
{
function __construct()
{
parent::Controller();
$this->load->library('option');
}
function index()
{
// to retrieve a value
echo $this->option->get('copyright');
// to set a new value
$this->option->update('copyright', '© 2009 Codenitive.');
}
}
$this->option->get()
Get a value of any given attribute available from the database table. If there’s no attribute with the given name return FALSE.
$data = $this->option->get('some_invalid_keyword');
// $data return FALSE
$this->option->update()
Set a new value for given attribute, or if it a new attribute create a record inside the database table.
$this->option->update('some_invalid_keyword', 'some valid');
Conclusion
How you can make use of it as I made in my project. If you manage to fine any bug please report it to us, Thank you.



Recent Comments