Methods of integrating Smarty and ADODB in CodeIgniter, codeignitersmarty_php tutorial

Source: Internet
Author: User
Tags dsn getting started with php codeigniter

Methods of integrating Smarty and ADODB in CodeIgniter, Codeignitersmarty


The examples in this paper describe the methods of integrating Smarty and ADODB in CodeIgniter. Share to everyone for your reference, as follows:

In CodeIgniter to write your own library, you need to write two files, one is in the application/init below the init_myclass.php file (if there is no init directory, created by itself). The other is to create a myclass.php file in the Application/libraries directory.

Here MyClass is your class name. Some rules people look at the handbook, I am here to directly say the steps.

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

<?php//load Smarty libraryrequire (' smarty/smarty.class.php ');//The setup.php file is a good place To load//required application the library files, and you//can do. An example://require (' guestbook/guestbook.lib.php '); class Mysmarty extends Smarty {function mysmarty () {//Class Con    Structor.    These automatically get set with the 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 the handy for development and Debugging;never are 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 '); }}?> 

File path is modified according to the situation, the path of the file is relative to the home directory of your website, not the current directory of the current file, such as the above require (' smarty/smarty.class.php '); not relative application/ Libraries directory, but relative $_server[' document_root '] directory.

The contents of the adodb.php file are 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 separately under the Application/init directory.

The contents of the init_adodb.php file are as follows:

<?php if (!defined (' BasePath ')) exit (' No Direct script access allowed '); $obj =& get_instance (); $obj->adodb = NE W Adodb ($obj); $obj->ci_is_loaded[] = ' Adodb ';

The contents of the init_mysmarty.php file are 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 you need in the Application/controllers directory, you can use ADODB and Smarty.

<?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 two times here. ADODB, according to the official practice should only need once, but his method in my here is wrong. Maybe I'm not quite familiar with CodeIgniter, so go deeper and see if there's a solution. But at least for now it's working.

More readers interested in PHP related content can view this site topic: "CodeIgniter Introductory Tutorial", "CI (codeigniter) Framework Advanced Tutorial", "PHP date and Time usage summary", "PHP object-oriented Programming introduction tutorial", "PHP string ( String) Usage summary, "Getting Started with Php+mysql database operation" and "PHP common database Operation Skills Summary"

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Using Smarty3 basic configuration in CodeIgniter
    • CodeIgniter How to connect, configure and use database
    • Analysis of Third_party usage of CodeIgniter Auxiliary third-party class library
    • Design defects and solutions of transaction processing for CodeIgniter framework database
    • Database configuration using CodeIgniter under the Sina SAE cloud Platform
    • CodeIgniter Integration Tank AUTH Permission class library detailed
    • Uploading images using CodeIgniter's class library
    • Summary of optimization of CodeIgniter Operation database table
    • CodeIgniter Database Operation Function Summary
    • How to make the CodeIgniter database cache automatic Expiration processing
    • CodeIgniter How to use the database class

http://www.bkjia.com/PHPjc/1106123.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106123.html techarticle The method of integrating Smarty and ADODB in CodeIgniter, Codeignitersmarty This example describes the methods of CodeIgniter and Smarty. Share to everyone for your reference, as follows: in ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.