《PHP編程:CI(Codeigniter)的Setting增強(qiáng)配置類實(shí)例》要點(diǎn):
本文介紹了PHP編程:CI(Codeigniter)的Setting增強(qiáng)配置類實(shí)例,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
PHP編程本文實(shí)例講述了Codeigniter的Setting增強(qiáng)配置類.分享給大家供大家參考,具體如下:
PHP編程該增強(qiáng)配置類適用配置項(xiàng)要求比較靈活的項(xiàng)目.可實(shí)現(xiàn)預(yù)加載配置、組配置、單項(xiàng)調(diào)取、增、刪、改配置,無(wú)需在改動(dòng)config文檔.
PHP編程使用:
PHP編程在需要的地方
PHP編程首先,創(chuàng)建數(shù)據(jù)表
PHP編程
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;
PHP編程然后,在application/libraries目錄下創(chuàng)建setting.php,內(nèi)容如下
PHP編程
<?php
if (!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();
}
// ------------------------------------------------------------------------
// 華麗的分割線 正式開始
// ------------------------------------------------------------------------
/**
* 從數(shù)據(jù)庫(kù)獲取所有自動(dòng)加載的設(shè)置
*/
public function autoload() {
//如果存在則直接返回
if (!empty($this->settings)) {
return $this->settings;
}
//如果系統(tǒng)不存在數(shù)據(jù)表則返回false
if (!$this->_ci->db->table_exists($this->settings_db)) {
return FALSE;
}
//查詢標(biāo)記為自動(dòng)加載的項(xiàng)
$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;
}
//循環(huán)寫入系統(tǒng)配置
foreach ($query->result() as $k => $row) {
$this->settings[$row->key] = $row->value;
$this->_ci->config->set_item($row->key, $row->value);
}
//標(biāo)記會(huì)話,避免重復(fù)讀庫(kù)
//$this->_ci->session->set_userdata('settings_autoloaded', TRUE);
return $this->settings;
}
// ------------------------------------------------------------------------
/**
* 獲取單個(gè)設(shè)定
*
* <code>
* <?php $this->settings->get('config_item');
?>
* </code>
*/
public function item($key) {
if (!$key) {
return FALSE;
}
//首先檢查是否系統(tǒng)已經(jīng)自動(dòng)加載
if (isset($this->settings[$key])) {
return $this->settings[$key];
}
//查詢數(shù)據(jù)庫(kù)
$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;
}
// 查詢不到結(jié)果則查找系統(tǒng)config,返回值或者false
return $this->_ci->config->item($key);
}
// ------------------------------------------------------------------------
/**
* 獲取組配置
*/
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;
}
// ------------------------------------------------------------------------
/**
* 更改設(shè)置
*/
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;
}
// ------------------------------------------------------------------------
/**
* 新增設(shè)置
*/
public function insert($key, $value = '', $group = 'addon', $autoload = 'no') {
// 檢查是否已經(jīng)被添加的設(shè)置
$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;
}
// ------------------------------------------------------------------------
/**
* 刪除設(shè)置
*/
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;
}
// ------------------------------------------------------------------------
/**
* 刪除設(shè)置組及成員配置
*/
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 */
PHP編程最后,打開application/config/config.php,新增
PHP編程
/**
* 系統(tǒng)配置表名
*/
$config['settings_table'] = "system_settings";
PHP編程希望本文所述對(duì)大家基于Codeigniter框架的PHP程序設(shè)計(jì)有所贊助.
《PHP編程:CI(Codeigniter)的Setting增強(qiáng)配置類實(shí)例》是否對(duì)您有啟發(fā),歡迎查看更多與《PHP編程:CI(Codeigniter)的Setting增強(qiáng)配置類實(shí)例》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.snjht.com/jiaocheng/7902.html