当前位置:首页 > PHP教程 > PHP常见问题

Codeigniter中集成smarty和adodb的方法

本文实例讲述了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 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'); } } ?>

文件路径根据具体情况修改,文件的的路径是相对你的网站的主目录开始的,而不是当前文件的当前目录,比如上面的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。

<?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,按照官方的做法应该只需要一次,但是他的方法在我这里有错误。可能是我对codeigniter还不太了解吧,等深入一些,再看看有没有解决办法。不过至少目前这个可以工作了。

更多关于php相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家php程序设计有所帮助。



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!