Codeigniter中整合smarty和adodb的方法_php執行個體

來源:互聯網
上載者:User

本文執行個體講述了Codeigniter中整合smarty和adodb的方法。分享給大家供大家參考,具體如下:

在CodeIgniter中要寫自己的庫,就需要寫兩個檔案,一個是在application/init下面的init_myclass.php檔案(如果沒有init目錄,自己建立)。另外一個就是在application/libraries目錄下建立myclass.php檔案。

這裡myclass是你的類名。一些規則大家看手冊就好了,我這裡直接就說步驟了。

1)在application/libraries下分別建立mysmarty.php和adodb.php
mysmarty.php檔案的內容如下:

<?php// load Smarty libraryrequire('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'); }}?>

檔案路徑根據具體情況修改,檔案的的路徑是相對你的網站的主目錄開始的,而不是當前檔案的目前的目錄,比如上面的require('Smarty/Smarty.class.php');不是相對application/libraries目錄,而是相對$_SERVER['DOCUMENT_ROOT']目錄。

adodb.php檔案的內容如下:

<?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目錄下分別建立init_adodb.php和init_mysmarty.php。

init_adodb.php檔案內容如下:

<?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檔案內容如下:

<?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目錄下建立一個你需要的檔案,你可以這樣來使用adodb和smarty。

<?phpclass 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,按照官方的做法應該只需要一次,但是他的方法在我這裡有錯誤。可能是我對CodeIgniter還不太瞭解吧,等深入一些,再看看有沒有解決辦法。不過至少目前這個可以工作了。

更多關於PHP相關內容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)架構進階教程》、《php日期與時間用法總結》、《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.