Codeigniter中集成smarty和adodb的方法
2016-03-04 00:00
681 查看
本文实例讲述了Codeigniter中集成smarty和adodb的方法。分享给大家供大家参考,具体如下:
在CodeIgniter中要写自己的库,就需要写两个文件,一个是在application/init下面的init_myclass.php文件(如果没有init目录,自己创建)。另外一个就是在application/libraries目录下创建myclass.php文件。
这里myclass是你的类名。一些规则大家看手册就好了,我这里直接就说步骤了。
1)在application/libraries下分别创建mysmarty.php和adodb.php
mysmarty.php文件的内容如下:
文件路径根据具体情况修改,文件的的路径是相对你的网站的主目录开始的,而不是当前文件的当前目录,比如上面的require('Smarty/Smarty.class.php');不是相对application/libraries目录,而是相对$_SERVER['DOCUMENT_ROOT']目录。
adodb.php文件的内容如下:
2)在application/init目录下分别创建init_adodb.php和init_mysmarty.php。
init_adodb.php文件内容如下:
init_mysmarty.php文件内容如下:
3)使用他们
在application/controllers目录下创建一个你需要的文件,你可以这样来使用adodb和smarty。
我也不知道这里为什么需要两次adodb,按照官方的做法应该只需要一次,但是他的方法在我这里有错误。可能是我对CodeIgniter还不太了解吧,等深入一些,再看看有没有解决办法。不过至少目前这个可以工作了。
更多关于PHP相关内容感兴趣的读者可查看本站专题:《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
CodeIgniter针对数据库的连接、配置及使用方法
CodeIgniter辅助之第三方类库third_party用法分析
CodeIgniter框架数据库事务处理的设计缺陷和解决方案
新浪SAE云平台下使用codeigniter的数据库配置
Codeigniter整合Tank Auth权限类库详解
使用CodeIgniter的类库做图片上传
Codeigniter操作数据库表的优化写法总结
codeigniter数据库操作函数汇总
让CodeIgniter数据库缓存自动过期的处理的方法
codeigniter自带数据库类使用方法说明
在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程序设计有所帮助。
您可能感兴趣的文章:
CodeIgniter中使用Smarty3基本配置CodeIgniter针对数据库的连接、配置及使用方法
CodeIgniter辅助之第三方类库third_party用法分析
CodeIgniter框架数据库事务处理的设计缺陷和解决方案
新浪SAE云平台下使用codeigniter的数据库配置
Codeigniter整合Tank Auth权限类库详解
使用CodeIgniter的类库做图片上传
Codeigniter操作数据库表的优化写法总结
codeigniter数据库操作函数汇总
让CodeIgniter数据库缓存自动过期的处理的方法
codeigniter自带数据库类使用方法说明
相关文章推荐
- 实现PHP框架系列文章(6)mysql数据库方法
- PHP结合Mysql数据库实现留言板功能
- zend framework中使用memcache的方法
- PHP截取IE浏览器并缩小原图的方法
- PHP使用fopen与file_get_contents读取文件实例分享
- PHP生成和获取XML格式数据的方法
- PHP使用curl模拟post上传及接收文件的方法
- php mailer类调用远程SMTP服务器发送邮件实现方法
- Zend Framework教程之Zend_Layout布局助手详解
- iOS本地化语言
- 快速掌握Lua 5.3 —— I/O库 (2)
- 通过Parcelable协议传递数据出现错误(Unmarshalling unknown type code 7471205 at offset 232)
- Win2D 入门教程 VB 中文版
- 【C#进阶系列】05 基元类型、引用类型和值类型
- Zend Framework教程之模型Model基本规则和使用方法
- 基于PHP实现等比压缩图片大小
- Zend Framework教程之模型Model用法简单实例
- jquery ajax双击div可直接修改div中的内容
- 基于Javascript实现二级联动菜单效果
- 基于zepto的移动端轻量级日期插件--date_picker