首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

CodeIgniter 整合 Smarty 與 ADOdb 完整步驟

     編輯:PHP 綜合知識

建立 Library 檔案

在 application/libraries/ 建立兩個檔案:

mysmarty.php

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->debugging = false;
        $this->cache_lifetime = 30;
        $this->caching = 0;
    }
}
?>

adodb.php

adodb =& ADONewConnection($dsn);
        $this->adodb->Execute("SET NAMES 'utf8'");
    }
}
?>

注意:require 路徑相對於網站根目錄 ($_SERVER['DOCUMENT_ROOT']),而非 libraries 目錄。

建立初始化檔案

在 application/init/ 建立:

init_mysmarty.php

mysmarty = new MySmarty();
$obj->ci_is_loaded[] = 'mysmarty';
?>

init_adodb.php

adodb = new Adodb($obj);
$obj->ci_is_loaded[] = 'adodb';
?>

控制器中使用

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');
    }
}

註:$this->adodb->adodb 雙層呼叫因 Library 實例屬性同名,官方建議單層但此處為相容寫法。

延伸閱讀

更多 CodeIgniter 進階教學請參考本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved