Codeigniter中集成smarty和adodb的方法
本文實(shí)例講述了Codeigniter中集成smarty和adodb的方法。分享給大家供大家參考,具體如下:
在CodeIgniter中要寫(xiě)自己的庫(kù),就需要寫(xiě)兩個(gè)文件,一個(gè)是在application/init下面的init_myclass.php文件(如果沒(méi)有init目錄,自己創(chuàng)建)。另外一個(gè)就是在application/libraries目錄下創(chuàng)建myclass.php文件。
這里myclass是你的類名。一些規(guī)則大家看手冊(cè)就好了,我這里直接就說(shuō)步驟了。
1)在application/libraries下分別創(chuàng)建mysmarty.php和adodb.php
mysmarty.php文件的內(nèi)容如下:
<?php // load Smarty library require('Smarty/Smarty.class.php'); // The setup.php file is a good place to load // required application library files, and you // can do that right here. An example: // require('guestbook/guestbook.lib.php'); class MySmarty extends Smarty { function MySmarty() { // Class Constructor. // These automatically get set with each new instance. $this->Smarty(); $basedir=dirname(__FILE__); $this->template_dir = "$basedir/templates/"; $this->compile_dir = "$basedir/templates_c/"; $this->config_dir = "$basedir/configs/"; $this->cache_dir = "$basedir/cache/"; //$this->compile_check = true; //this is handy for development and debugging;never be used in a production environment. //$smarty->force_compile=true; $this->debugging = false; $this->cache_lifetime=30; $this->caching = 0; // lifetime is per cache //$this->assign('app_name', 'Guest Book'); } } ?>
文件路徑根據(jù)具體情況修改,文件的的路徑是相對(duì)你的網(wǎng)站的主目錄開(kāi)始的,而不是當(dāng)前文件的當(dāng)前目錄,比如上面的require('Smarty/Smarty.class.php');不是相對(duì)application/libraries目錄,而是相對(duì)$_SERVER['DOCUMENT_ROOT']目錄。
adodb.php文件的內(nèi)容如下:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Adodb { function Adodb() { //$dsn="dbdriver://username:password@server/database" $dsn = 'mysql://user:password@localhost/xxxx'; require_once("adodb/adodb.inc".EXT); $this->adodb =& ADONewConnection($dsn); $this->adodb->Execute("set NAMES 'utf8'"); } } ?>
2)在application/init目錄下分別創(chuàng)建init_adodb.php和init_mysmarty.php。
init_adodb.php文件內(nèi)容如下:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); $obj =& get_instance(); $obj->adodb = new Adodb($obj); $obj->ci_is_loaded[] = 'adodb';
init_mysmarty.php文件內(nèi)容如下:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); if ( ! class_exists('MySmarty')) { require_once(APPPATH.'libraries/mysmarty'.EXT); } $obj =& get_instance(); $obj->mysmarty = new MySmarty(); $obj->ci_is_loaded[] = 'mysmarty'; ?>
3)使用他們
在application/controllers目錄下創(chuàng)建一個(gè)你需要的文件,你可以這樣來(lái)使用adodb和smarty。
<?php class Test extends Controller { function Test() { parent::Controller(); $this->load->library('mysmarty'); $this->load->library('adodb'); } function index() { $this->load->library('adodb'); $row = $this->adodb->adodb->getrow('SELECT * FROM admin'); $this->mysmarty->assign("row",$row); $this->mysmarty->display("test.tpl"); } } ?>
我也不知道這里為什么需要兩次adodb,按照官方的做法應(yīng)該只需要一次,但是他的方法在我這里有錯(cuò)誤。可能是我對(duì)CodeIgniter還不太了解吧,等深入一些,再看看有沒(méi)有解決辦法。不過(guò)至少目前這個(gè)可以工作了。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門(mén)教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
Codeigniter實(shí)現(xiàn)多文件上傳并創(chuàng)建多個(gè)縮略圖
這篇文章主要介紹了Codeigniter實(shí)現(xiàn)多文件上傳并創(chuàng)建多個(gè)縮略圖,需要的朋友可以參考下2014-06-06微信公眾平臺(tái)開(kāi)發(fā)(五) 天氣預(yù)報(bào)功能開(kāi)發(fā)
這篇文章主要介紹了微信公眾平臺(tái)開(kāi)發(fā)(五) 天氣預(yù)報(bào)功能開(kāi)發(fā) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12PHP框架實(shí)現(xiàn)WebSocket在線聊天通訊系統(tǒng)
這篇文章主要介紹了PHP框架結(jié)合實(shí)現(xiàn)WebSocket在線聊天通訊系統(tǒng),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11ThinkPHP實(shí)現(xiàn)ajax仿官網(wǎng)搜索功能實(shí)例
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)ajax仿官網(wǎng)搜索功能的方法,實(shí)例演示了后臺(tái)查詢功能與前臺(tái)Ajax提交搜索數(shù)據(jù)的方法,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12PHP實(shí)現(xiàn)微信公眾平臺(tái)音樂(lè)點(diǎn)播
首先說(shuō)一下思路,微信提供了接口,只要數(shù)據(jù)格式滿足它所給的接口的XML格式即可以發(fā)送給關(guān)注者對(duì)應(yīng)的音樂(lè)2014-03-03postman的安裝與使用方法(模擬Get和Post請(qǐng)求)
今天小編就為大家分享一篇postman的安裝與使用方法(模擬Get和Post請(qǐng)求),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08