This article mainly introduces how to integrate smarty and adodb in Codeigniter, and analyzes the usage skills of the Codeigniter library based on examples, for more information about how to integrate smarty and adodb in Codeigniter, see the following example. We will share this with you for your reference. The details are as follows:
To write your own library in CodeIgniter, you need to write two files, one is the init_myclass.php file under application/init (if there is no init Directory, create it yourself ). The other is to create the myclass. php file under the application/libraries Directory.
Here, myclass is your class name. You can read the manual for some rules. here I will talk about the steps directly.
1) create mysmarty. php and adodb. php under application/libraries respectively
The content of the mysmarty. php file is as follows:
<?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'); }}?>
The file path is modified according to the actual situation. the file path starts with the home directory of your website, rather than the current directory of the current file, such as the above require ('smarty/Smarty. class. php '); instead of the application/libraries directory, it is relative to the $ _ SERVER ['document _ root'] Directory.
The content of the adodb. php file is as follows:
<?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) create init_adodb.php and init_mysmarty.php under the application/init directory respectively.
The content of the init_adodb.php file is as follows:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');$obj =& get_instance();$obj->adodb = new Adodb($obj);$obj->ci_is_loaded[] = 'adodb';
The content of the init_mysmarty.php file is as follows:
<?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) use them
Create a file in the application/controllers Directory. you can use adodb and smarty in this way.
<?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"); }}?>
I don't know why I need to use adodb twice here. I should only need it once according to the official practice, but his method is wrong here. Maybe I don't know much about CodeIgniter. wait for more details and see if there is any solution. However, at least this can work now.