這篇文章主要介紹了Codeigniter中如何集成smarty和adodb,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
在CodeIgniter中要寫自己的庫,就需要寫兩個文件,一個是在application/init下面的init_myclass.php文件(如果沒有init目錄,自己創(chuàng)建)。另外一個就是在application/libraries目錄下創(chuàng)建myclass.php文件。
這里myclass是你的類名。一些規(guī)則大家看手冊就好了,我這里直接就說步驟了。
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ù)具體情況修改,文件的的路徑是相對你的網(wǎng)站的主目錄開始的,而不是當(dāng)前文件的當(dāng)前目錄,比如上面的require('Smarty/Smarty.class.php');不是相對application/libraries目錄,而是相對$_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)建一個你需要的文件,你可以這樣來使用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)該只需要一次,但是他的方法在我這里有錯誤??赡苁俏覍odeIgniter還不太了解吧,等深入一些,再看看有沒有解決辦法。不過至少目前這個可以工作了。
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Codeigniter中如何集成smarty和adodb”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
標(biāo)題名稱:Codeigniter中如何集成smarty和adodb-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://chinadenli.net/article20/dgjjjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、小程序開發(fā)、網(wǎng)站設(shè)計、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容