Setting of CI (Codeigniter) enhanced configuration class instance _ php instance

Source: Internet
Author: User
Tags autoload change settings
This article mainly introduces the Setting enhanced configuration class of Codeigniter, and analyzes in detail the implementation steps and related skills of the Codeigniter enhanced configuration class based on the instance form, for more information about the Setting enhanced configuration class of Codeigniter, see the example in this article. We will share this with you for your reference. The details are as follows:

This enhanced configuration class applies to projects with flexible configuration requirements. You can perform preload configuration, group configuration, single-item retrieval, addition, deletion, and Configuration modification without modifying the config document.

Usage:

Where needed

The Code is as follows:

$ This-> load-> library ('setting ');


Pre-add-ons can be used

The Code is as follows:

$ This-> config-> item ();

Obtain
You can use

The Code is as follows:

$ This-> setting-> item ();

Obtain

First, create a data table

CREATE TABLE `system_settings` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `key` varchar(64) NOT NULL DEFAULT '', `value` mediumtext NOT NULL, `group` varchar(55) NOT NULL DEFAULT 'site', `autoload` enum('no','yes') NOT NULL DEFAULT 'yes', PRIMARY KEY (`id`,`key`), KEY `name` (`key`), KEY `autoload` (`autoload`)) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

Then, create setting. php In the application/libraries directory. The content is as follows:

<? Phpif (! Defined ('basepath') exit ('no direct script access allowed'); class Setting {private $ _ ci; private $ settings_autoloaded; private $ settings = array (); private $ settings_group = array (); private $ settings_db; public function _ construct () {$ this-> _ ci = & get_instance (); $ this-> settings_db = $ this-> _ ci-> config-> item ('settings _ table'); $ this-> autoload ();}//------------------------------------------- --------------------------- // The gorgeous split line is officially started. // extract/*** get all automatically loaded settings from the database */public function autoload () {// if yes, if (! Empty ($ this-> settings) {return $ this-> settings;} // returns false if (! $ This-> _ ci-> db-> table_exists ($ this-> settings_db) {return FALSE ;} // items marked as automatically loaded in the query $ this-> _ ci-> db-> select ('key, value')-> from ($ this-> settings_db) -> where ('autoload', 'yes'); $ query = $ this-> _ ci-> db-> get (); if ($ query-> num_rows () = 0) {return FALSE;} // configure the system for cyclic writing ($ query-> result () as $ k => $ row) {$ this-> settings [$ row-> key] = $ row-> value; $ this-> _ ci-> config-> set_item ($ row-> key, $ row-> value);} // mark the session to avoid reading the database repeatedly // $ this-> _ ci-> session-> set_userdata ('settings _ autoloaded', TRUE ); return $ this-> settings;} // ----------------------------------------------------------------------------/*** get a single setting **   * <?php $this->settings->get('config_item');   ?>   * */Public function item ($ key) {if (! $ Key) {return FALSE;} // first check whether the system has automatically loaded if (isset ($ this-> settings [$ key]) {return $ this-> settings [$ key];} // query the database $ this-> _ ci-> db-> select ('value ') -> from ($ this-> settings_db)-> where ('key', $ key); $ query = $ this-> _ ci-> db-> get (); if ($ query-> num_rows ()> 0) {$ row = $ query-> row (); $ this-> settings [$ key] = $ row-> value; return $ row-> value;} // if no query result is found, search for system config. If the returned value is false, return $ this-> _ ci-> config-> ite. M ($ key) ;}// --------------------------------------------------------------------------/*** get group configuration */public function group ($ group = '') {if (! $ Group) {return FALSE;} $ this-> _ ci-> db-> select ('key, value')-> from ($ this-> settings_db) -> where ('group', $ group); $ query = $ this-> _ ci-> db-> get (); if ($ query-> num_rows () = 0) {return FALSE;} foreach ($ query-> result () as $ k => $ row) {$ this-> settings [$ row-> key] = $ row-> value; $ arr [$ row-> key] = $ row-> value ;} return $ arr;} // --------------------------------------------------------------------------/*** change settings */public function edit ($ key, $ value) {$ this-> _ ci-> db-> where ('key', $ key ); $ this-> _ ci-> db-> update ($ this-> settings_db, array ('value' => $ value )); if ($ this-> _ ci-> db-> affected_rows () = 0) {return FALSE;} return TRUE ;} // Configure/*** add setting */public function insert ($ key, $ value = '', $ group = 'addon ', $ autoload = 'no ') {// check whether the settings have been added $ this-> _ ci-> db-> select ('value')-> from ($ this-> settings_db) -> where ('key', $ key); $ query = $ this-> _ ci-> db-> get (); if ($ query-> num_rows ()> 0) {return $ this-> edit ($ key, $ value);} $ data = array ('key' => $ key, 'value' => $ value, 'group' => $ group, 'autoload' => $ autoload,); $ this-> _ ci-> db-> insert ($ this-> settings_db, $ data); if ($ this-> _ ci-> db-> affected_rows () = 0) {return FALSE;} return TRUE ;} // deletion/*** delete setting */public function delete ($ key) {$ this-> _ ci-> db-> delete ($ this-> settings_db, array ('key' => $ key); if ($ this-> _ ci-> db-> affected_rows () = 0) {return FALSE ;} return TRUE;} // --------------------------------------------------------------------------/*** Delete the Configuration group and Member configuration */public function delete_group ($ group) {$ this-> _ ci-> db-> delete ($ this-> settings_db, array ('group' => $ group )); if ($ this-> _ ci-> db-> affected_rows () = 0) {return FALSE;} return TRUE ;}/ * End of file Setting. php * // * Location :. /application/libraries/Setting. php */

Finally, open application/config. php and add

/*** System configuration table name */$ config ['settings _ table'] = "system_settings ";

I hope this article will help you design PHP programs based on the Codeigniter framework.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.