Smarty instance tutorial (5)

Source: Internet
Author: User
Tags mysql functions
Simply replace NewsTitle and NewsContent on the news page with the content found in the database.

Next let's take a look at the source file of news. php:

PHP code:
--------------------------------------------------------------------------------
========================================================== ======================
News. php
========================================================== ======================

Php code:

 

The above program statements about Smarty mainly have three rows:




========================================================== ======================================
$ Smarty-> assign ("NewsTitle", $ row ["vcNewsTitle"]);
$ Smarty-> assign ("NewsContent", $ row ["ltNewsContent"]);

Mysql_free_result ($ result );
========================================================== ======================================

--------------------------------------------------------------------------------

Simply replace NewsTitle and NewsContent on the news page with the content found in the database.

Well, the first part of the Smarty routine learning is here. I believe you will have a basic understanding of the usage of Smarty after learning it, the next section also uses this example to explain how to use the DB class in phplib to implement template control.


========================================================== ========================================================== ================================



Smarty instance Tutorial example (2. use the phplib DB Class)




There are some things a few days ago, so this instance has been written till today. why should I use the DB class in phplib? When learning PHP, many people may first come to use the phplib template. The reason is very simple: PHPLIB is introduced in many PHP learning materials, and phplib is very popular in PHP3, one of the reasons is that it implements the session function that is not implemented in PHP3, until now many people still use the phplib template technology, because it is very simple. At the same time, database operation classes on phplib are also popular with small files and fast loading speed.

. I like its syntax and feel very similar to mysql statements in PHP. Okay. let's talk about our program. The program will also talk about the site in instance 1. we will not talk about database establishment and template establishment here. please refer to the introduction in the previous section, here we will introduce new things added in the program.

First, let's look at the directory table:
+ Web (site root directory)
|
| ---- + Comm (Smarty Documentation Directory)
|
| ---- + Plugins (Smarty plug-in directory)
| ----- Config_File.class.php (Smarty configuration file)
| ----- Smarty. class. php (master Smarty file)
| ----- Smarty_Compiler.class.php (Smarty compilation class file)
| ----- Db_mysql.inc.php (DB class in phplib)
| ----- Csub. inc. php (a function that intercepts Chinese characters)
|
| ---- + Cache (Smarty cache directory, which guarantees read and write permissions under * nix)
|
| ---- + Templates (website template file storage directory)
|
| ---- Header. tpl (page header template file)
| ---- Index. tpl (website homepage template file)
| ---- Foot. tpl (page footer template file)
| ---- News. tpl (news page template file)
|
|
| ---- + Templates_c (directory for storing template files after compilation, and read/write permission for * nix)
|
| ---- + Css (site CSS file directory)
|
| ---- + Image (site image directory)
|
| ---- + Media (website Flash animation storage directory)
|
| ----Indexbak.htm (original homepage)
|
| ---- Newsbk, htm (original news page)
|
| ---- Index. php (Smarty homepage program file)
|
| ---- News. php (Smarty news display file)
|
| ------Example .txt (this document)

Here there are mainly two more files in the/comm/directory, one is db_mysql.inc.php, and its file name in phplib is db_mysql.inc, by default, the inc file will be displayed in the browser when it is located directly from the browser. Therefore, for security, we should change its extension to db_mysql.inc.php; the second file is a function that contains Chinese characters.

Csubstr (). the Source Code is displayed when it is used.
Here we will give a brief introduction to the main member variables and member functions defined in db_mysql.inc.php:
I. member variables:
1. $ Host: Host name
2. $ Database: Database name
3. $ User: User name
4. $ Password: Password
II. member functions:
1. connect ($ Database = "", $ Host = "", $ User = "", $ Password = ""): creates a connection and returns the connection ID.
2. query ($ Query_String): query. the query ID is returned.
3. free (): Release the current query ID resource
4. next_record (): returns the next return set.
5. num_rows (): number of data rows in the current query number
6. f ($ name): the value of the current field

Other functions are rarely used in our program, so we will not introduce them. interested readers can view relevant information on their own.

PHP code :--------------------------------------------------------------------------------

The index. tpl and news. tpl in templates are quite the same as those in the previous section. The difference is that in index. php and news. tpl, let's take a look at index. php:

====================================
Index. php
====================================

Php code:



Old rules: I added a label to the key points. let's look at these points:
1. here the DB_ SQL class file is included, and the following line is to include a Chinese truncation class to the current page
2. instantiate a class, set the Host, Database, User, and Password attributes of the class, and connect the Database.
3. call $ db-> query ($ strQuery) for a data query
4. $ db-> next_record () reads the next row returned by the database query (note: Call query () to return the previous record of the first record of the dataset)
5. csubstr () is a Chinese truncation class. the source file of the class is as follows:
========================================================== ==================
Csubstr. inc. php
========================================================== ==================


6. $ db-> free (): Release the resources returned by the current query.

Well, using classes for database operations can greatly improve program readability and facilitate database operations.
Next let's take a look at news. php:
========================================================== ==========
News. php
========================================================== ==========

Php code:

 

 


Let's take a look at the steps 1 and 2. is it simpler than using mysql functions directly?
If you have carefully read the previous example, this article should be very understandable.

======================================
Postscript:
The template part is basically based on the examples in the manual. it is embodied in some, and the actual essence is in the program design part. to learn it, we should look at two examples of programs, I put two routines and the example programs mentioned earlier in three folders and have tested them. you can view and debug them.

 


/*************************************** ******
*
* File name: news. php
* For use: news Display program
*
* Author: Master
* Email: teacherli@163.com
*
**************************************** *****/
Include_once ("./comm/Smarty. class. php"); // contains the smarty class file
// ================================================ =
Include_once ("./comm/db_mysql.inc.php"); // contains the database operation class
Include_once ("./comm/csubstr. inc"); // contains the Chinese truncation class
// ================================================ =
Define ("NUM", 5); // defines the number of news entries displayed each time.

$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> templates_dir = "./templates"; // set the template directory
$ Smarty-> compile_dir = "./templates_c"; // Set the compilation Directory
$ Smarty-> cache_dir = "./cache"; // you can specify a cache directory.
$ Smarty-> cache_lifetime = 60*60*24; // you can specify the cache time.
$ Smarty-> caching = false; // set this parameter to false during debugging. use true for release.
$ Smarty-> left_delimiter = "<{"; // you can specify the left boundary.
$ Smarty-> right_delimiter = "}

> "; // Set the right boundary


$ Db = new DB_ SQL (); // instantiate a DB Class
$ Db-> Host = "localhost"; // database Host name
$ Db-> Database = "news"; // Database name
$ Db-> User = "root"; // User name
$ Db-> Password = ""; // Password

$ Db-> connet (); // connect to the database

$ NewsID = $ _ GET ["id"]; // Obtain the news id
$ NewsType = $ _ GET ["type"]; // The type of news to be displayed
Switch ($ NewsType)
{
Case 1:
$ DbName = "tb_news_ch ";
Break;
Case 2:
$ DbName = "tb_news_in ";
Break;
Case 3:
$ DbName = "tb_news_mu ";
Break;
}

$ StrQuery = "SELECT vcNewsTitle, ltNewsContent FROM". $ dbName;
1. $ db-> query ($ strQuery );
2. if ($ db-> next_record ())
{
$ Smarty-> assign ("NewsTitle", $ db-> f ("vcNewsTitle "));
$ Smarty-> assign ("NewsContent", $ db-> f ("ltNewsContent "));

$ Db-> free ();
$ Smarty-> display ("news. tpl ");
} Else
{
Echo "no news content ";
}

?>



/*************************************** ******
*
* File name: index. php
* For use: Display instance programs
*
* Author: Master
* Email: teacherli@163.com
*
**************************************** *****/
Include_once ("./comm/Smarty. class. php"); // contains the smarty class file
1. // ================================================ =
Include_once ("./comm/db_mysql.inc.php"); // contains the database operation class
Include_once ("./comm/csubstr. inc"); // contains the Chinese truncation class
// ================================================ =
Define ("NUM", 5); // defines the number of news entries displayed each time.

$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> templates_dir = "./templates"; // set the template directory
$ Smarty-> compile_dir = "./templates_c"; // Set the compilation Directory
$ Smarty-> cache_dir = "./cache"; // you can specify a cache directory.
$ Smarty-> caching = false; // set this parameter to false during debugging. use true for release.
$ Smarty-> left_delimiter = "<{"; // you can specify the left boundary.
$ Smarty-> right_delimiter = "}>"; // you can specify the right boundary.


2. $ db = new DB_ SQL (); // instantiate a DB Class
$ Db-> Host = "localhost"; // database Host name
$ Db-> Database = "news"; // Database name
$ Db-> User = "root"; // User name
$ Db-> Password = ""; // Password

$ Db-> connet (); // connect to the database

// The domestic news section will be processed here
$ StrQuery = "SELECT iNewsID, iNewsTitle FROM tb_news_CH order by iNewsID DESC ";
3. $ db-> query ($ strQuery );
$ I = NUM;
4. while ($ db-> next_record () & $ I> 0)
{
5. $ array [] = array ("NewsID", csubstr ($ db-> f ("iNewsID"), 0, 20 ),
"NewsTitle", csubstr ($ db-> f ("vcNewsTitle"), 0, 20 ));

$ I --;
}
$ Smarty-> assign ("News_CH", $ array );
Unset ($ array );
6. $ db-> free ();


// Process International News
$ StrQuery = "SELECT iNewsID, iNewsTitle FROM tb_news_IN order by iNewsID DESC ";
$ Db-> query ($ strQuery );
$ I = NUM;
While ($ db-> next_record () & $ I> 0)
{
$ Array [] = array ("NewsID", csubstr ($ db-> f ("iNewsID"), 0, 20 ),
"NewsTitle", csubstr ($ db-> f ("vcNewsTitle"), 0, 20 ));

$ I --;
}
$ Smarty-> assign ("News_IN", $ array );
Unset ($ array );
$ Db-> free ();


// The entertainment news section will be processed here
$ StrQuery = "SELECT iNewsID, iNewsTitle FROM tb_news_MU order by iNewsID DESC ";
$ Db-> query ($ strQuery );
$ I = NUM;
While ($ db-> next_record () & $ I> 0)
{
$ Array [] = array ("NewsID", csubstr ($ db-> f ("iNewsID"), 0, 20 ),
"NewsTitle", csubstr ($ db-> f ("vcNewsTitle"), 0, 20 ));

$ I --;
}
$ Smarty-> assign ("News_MU", $ array );
Unset ($ array );
$ Db-> free ();


// Compile and display the index. tpl template under./templates
$ Smarty-> display ("index. tpl ");
?>


/*************************************** ******
*
* File name: news. php
* For use: news Display program
*
* Author: Master
* Email: teacherli@163.com
*
**************************************** *****/
Include_once ("./comm/Smarty. class. php"); // contains the smarty class file
Define ("NUM", 5); // defines the number of news entries displayed each time.

$ Smarty = new Smarty (); // Create a smarty instance object $ smarty
$ Smarty-> templates_dir = "./templates"; // set the template directory
$ Smarty-> compile_dir = "./templates_c"; // Set the compilation Directory
$ Smarty-> cache_dir = "./cache"; // you can specify a cache directory.
$ Smarty-> cache_lifetime = 60*60*24; // you can specify the cache time.
$ Smarty-> caching = false; // set this parameter to false during debugging. use true for release.
$ Smarty-> left_delimiter = "<{"; // you can specify the left boundary.
$ Smarty-> right_delimiter = "}>"; // you can specify the right boundary.


$ Db = mysql_connect ("localhost", "root", "") or die ("database connection error! ");
Mysql_select_db ("News", $ db );

$ NewsID = $ _ GET ["id"]; // Obtain the news id
$ NewsType = $ _ GET ["type"]; // The type of news to be displayed
Switch ($ NewsType)
{
Case 1:
$ DbName = "tb_news_ch ";
Break;
Case 2:
$ DbName = "tb_news_in ";
Break;
Case 3:
$ DbName = "tb_news_mu ";
Break;
}

$ StrQuery = "SELECT vcNewsTitle, ltNewsContent FROM". $ dbName;
$ Result = mysql_query ($ strQuery) or die ("database query error! ");
If ($ row = mysql_fetch_array ($ result ))
{
$ Smarty-> assign ("NewsTitle", $ row ["vcNewsTitle"]);
$ Smarty-> assign ("NewsContent", $ row ["ltNewsContent"]);

Mysql_free_result ($ result );
$ Smarty-> display ("news. tpl ");
}

Mysql_close ($ db );

?>

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.