CI (CodeIgniter) setting enhanced configuration class instance, cicodeigniter_php tutorial

Source: Internet
Author: User
Tags autoload change settings codeigniter

CI (CodeIgniter) setting enhanced configuration class instance, Cicodeigniter


This example describes the setting enhanced configuration class for CodeIgniter. Share to everyone for your reference, as follows:

The enhanced configuration class is suitable for items that require more flexible configuration items. It can implement pre-load configuration, group configuration, single tune, add, delete, change configuration without changing config document.

Use:

where it's needed.
Copy the Code code as follows: $this->load->library (' setting ');
For pre-add-ons You can use the copy code code as follows: $this->config->item (); get
You can use the copy code code for a temporary fetch entry as follows: $this->setting->item (); get

First, create a data table

CREATE TABLE ' system_settings ' (' id ' int (one) unsigned not null auto_increment, ' key ' varchar ($) ' NOT null DEFAULT ' ', ' VA  Lue ' mediumtext not NULL, ' group ' varchar (ON) NOT null default ' site ', ' AutoLoad ' enum (' No ', ' yes ') ' and ' not ' null default ' Yes ', PRIMARY key (' id ', ' key '), key ' name ' (' key '), key ' AutoLoad ' (' AutoLoad ')) Engine=myisam auto_increment=11 DEFAULT Charse T=utf8;

Then create setting.php under the Application/libraries directory, 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 (); }//------------------------------------------------------------------------//Ornate Split line officially started//--------------------- ---------------------------------------------------/** * Get all auto-loaded settings from the database */Public Function AutoLoad () {//if present    return directly if (!empty ($this->settings)) {return $this->settings;    }//If the system does not exist the data table returns False if (! $this->_ci->db->table_exists ($this->settings_db)) {return false; }//The query marked as an automatically loaded item $this->_ci->db->select (' Key,value ')->from ($this->settings_db)->where ('    AutoLoad ', ' yes '); $query = $this->_ci-&Gt;db->get ();    if ($query->num_rows () = = 0) {return FALSE; }//Cyclic write system configuration foreach ($query->result () as $k = = $row) {$this->settings[$row->key] = $row->value      ;    $this->_ci->config->set_item ($row->key, $row->value);    }//Mark session, avoid duplicate read library//$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 if the system has automatically loaded if (Isset ($this->settings[$key]) {return $this->settings[$key];    }//Query 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;  }//The query does not reach the result to find the System config, return value or False return $this->_ci->config->item ($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 ($k    EY, $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; }//------------------------------------------------------------------------/** * New settings */Public Function Insert ( $key, $value = ', $group = ' addon ', $autoload = ' no ') {//check if the setting has been added $this->_ci->db->select (' value ')-&G    T;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; }//------------------------------------------------------------------------/** * Delete settings */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 settings group and member configuration */Public function D    Elete_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 the application/config/config.php and add

/** * System Configuration Table name */$config [' settings_table '] = "system_settings";

It is hoped that this article is helpful to the PHP program design based on CodeIgniter framework.

Articles you may be interested in:

    • Using Smarty3 basic configuration in CodeIgniter
    • CodeIgniter Framework method under Nginx configuration
    • Nginx support codeigniter pathinfo mode URL Rewrite configuration example
    • Database configuration using CodeIgniter under the Sina SAE cloud Platform
    • Defining CodeIgniter Global variables using configuration classes
    • CI (codeigniter) Framework Configuration
    • CodeIgniter Basic Configuration Detailed Introduction
    • Parsing the CodeIgniter custom configuration file

http://www.bkjia.com/PHPjc/1089204.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089204.html techarticle CI (CodeIgniter) setting enhanced configuration class instance, Cicodeigniter This example describes the setting enhanced configuration class for CodeIgniter. Share to everyone for your reference, as follows: The enhanced match ...

  • 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.