Home > Backend Development > PHP Tutorial > How to integrate smarty and adodb in Codeigniter, codeignitersmarty_PHP tutorial

How to integrate smarty and adodb in Codeigniter, codeignitersmarty_PHP tutorial

WBOY
Release: 2016-07-12 08:57:55
Original
845 people have browsed it

How to integrate smarty and adodb in Codeigniter, codeignitersmarty

This article describes the method of integrating smarty and adodb in Codeigniter. Share it with everyone 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 in the application/libraries directory.

Here myclass is your class name. You can just read the manual for some rules. I will just explain the steps here.

1) Create mysmarty.php and adodb.php respectively under application/libraries
The contents of the mysmarty.php file are as follows:

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

Copy after login

The file path is modified according to the specific situation. The file path starts relative to the main directory of your website, not the current directory of the current file. For example, the above require('Smarty/Smarty.class.php'); is not Relative to the application/libraries directory, but relative to the $_SERVER['DOCUMENT_ROOT'] directory.

The content of the adodb.php file is as follows:

<&#63;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'"); 
  }
}
&#63;>

Copy after login

2) Create init_adodb.php and init_mysmarty.php respectively in the application/init directory.

The content of the init_adodb.php file is as follows:

<&#63;php if (!defined('BASEPATH')) exit('No direct script access allowed');
$obj =& get_instance();
$obj->adodb = new Adodb($obj);
$obj->ci_is_loaded[] = 'adodb';

Copy after login

The content of the init_mysmarty.php file is as follows:

<&#63;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';
&#63;>

Copy after login

3) Use them
Create a file you need in the application/controllers directory. You can use adodb and smarty like this.

<&#63;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");
 }
}
&#63;>

Copy after login

I don’t know why adodb is needed twice here. According to the official method, it should only be needed once, but his method is wrong here. Maybe it's because I don't know much about CodeIgniter yet. I'll see if there is a solution if I dig deeper. But at least this one works for now.

Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction to codeigniter tutorial", "CI (CodeIgniter) framework advanced tutorial", "php date and time usage summary", "php object-oriented program" Design introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Using Smarty3 basic configuration in CodeIgniter
  • CodeIgniter connection, configuration and usage methods for databases
  • CodeIgniter assistance Usage analysis of third-party class library third_party
  • Design flaws and solutions for CodeIgniter framework database transaction processing
  • Database configuration using codeigniter under Sina SAE cloud platform
  • Codeigniter integrates Tank Auth Detailed explanation of permission class library
  • Using CodeIgniter class library to upload images
  • A summary of the optimized writing method of Codeigniter operation database table
  • A summary of codeigniter database operation functions
  • Let How to handle automatic expiration of CodeIgniter database cache
  • Instructions for using codeigniter’s own database class

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1106123.htmlTechArticleHow to integrate smarty and adodb in Codeigniter, codeignitersmarty This article describes the method of integrating smarty and adodb in Codeigniter. Share it with everyone for your reference, the details are as follows: In...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template