本文实例讲述了Codeigniter的Setting增强配置类。分享给大家供大家参考,具体如下:
该增强配置类适用配置项要求比较灵活的项目。可实现预加载配置、组配置、单项调取、增、删、改配置,无需在改动config文档。
使用:
在需要的地方
代码如下:
$this->load->library('setting');
对于预加载项可以使用
代码如下:
$this->config->item();
进行获取对于临时调取项可以使用
代码如下:
$this->setting->item();
进行获取首先,创建数据表
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;
然后,在application/libraries目录下创建setting.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(); } // ------------------------------------------------------------------------ // 华丽的分割线 正式开始 // ------------------------------------------------------------------------ /** * 从数据库获取所有自动加载的设置 */ public function autoload() { //如果存在则直接返回 if (!empty($this->settings)) { return $this->settings; } //如果系统不存在数据表则返回false if (!$this->_ci->db->table_exists($this->settings_db)) { return FALSE; } //查询标记为自动加载的项 $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; } //循环写入系统配置 foreach ($query->result() as $k => $row) { $this->settings[$row->key] = $row->value; $this->_ci->config->set_item($row->key, $row->value); } //标记会话,避免重复读库 //$this->_ci->session->set_userdata('settings_autoloaded', TRUE); return $this->settings; } // ------------------------------------------------------------------------ /** * 获取单个设定 * * <code> * <?php $this->settings->get('config_item'); ?> * </code> */ public function item($key) { if (!$key) { return FALSE; } //首先检查是否系统已经自动加载 if (isset($this->settings[$key])) { return $this->settings[$key]; } //查询数据库 $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; } // 查询不到结果则查找系统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; } // ------------------------------------------------------------------------ /** * 更改设置 */ 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; } // ------------------------------------------------------------------------ /** * 新增设置 */ public function insert($key, $value = '', $group = 'addon', $autoload = 'no') { // 检查是否已经被添加的设置 $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; } // ------------------------------------------------------------------------ /** * 删除设置 */ 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; } // ------------------------------------------------------------------------ /** * 删除设置组及成员配置 */ 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 */
最后,打开application/config/config.php,新增
/** * 系统配置表名 */ $config['settings_table'] = "system_settings";
希望本文所述对大家基于Codeigniter框架的PHP程序设计有所帮助。

PHP仍然流行的原因是其易用性、靈活性和強大的生態系統。 1)易用性和簡單語法使其成為初學者的首選。 2)與web開發緊密結合,處理HTTP請求和數據庫交互出色。 3)龐大的生態系統提供了豐富的工具和庫。 4)活躍的社區和開源性質使其適應新需求和技術趨勢。

PHP和Python都是高層次的編程語言,廣泛應用於Web開發、數據處理和自動化任務。 1.PHP常用於構建動態網站和內容管理系統,而Python常用於構建Web框架和數據科學。 2.PHP使用echo輸出內容,Python使用print。 3.兩者都支持面向對象編程,但語法和關鍵字不同。 4.PHP支持弱類型轉換,Python則更嚴格。 5.PHP性能優化包括使用OPcache和異步編程,Python則使用cProfile和異步編程。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP起源於1994年,由RasmusLerdorf開發,最初用於跟踪網站訪問者,逐漸演變為服務器端腳本語言,廣泛應用於網頁開發。 Python由GuidovanRossum於1980年代末開發,1991年首次發布,強調代碼可讀性和簡潔性,適用於科學計算、數據分析等領域。

PHP適合網頁開發和快速原型開發,Python適用於數據科學和機器學習。 1.PHP用於動態網頁開發,語法簡單,適合快速開發。 2.Python語法簡潔,適用於多領域,庫生態系統強大。

PHP在現代化進程中仍然重要,因為它支持大量網站和應用,並通過框架適應開發需求。 1.PHP7提升了性能並引入了新功能。 2.現代框架如Laravel、Symfony和CodeIgniter簡化開發,提高代碼質量。 3.性能優化和最佳實踐進一步提升應用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver CS6
視覺化網頁開發工具