Before reading this article, we recommend that you refer to "accessing MySql database with PHP". Smarty is a template engine written in the PHP language and one of the most famous PHP template engines in the industry. It separates the logic code from the external content and blends the original PHP code with the HTML code into... SyntaxHighlighter. all ();
Before reading this article, we recommend that you refer to "accessing MySql database with PHP".
Smarty is a template engine written in the PHP language and one of the most famous PHP template engines in the industry. It separates the logic code from the external content, and separates the PHP code from the HTML code. So that PHP programmers and front-end programmers of the website can achieve a good division of labor-PHP programmers change the logic content of the program does not affect the front-end staff's page design, the re-modification of the page style by the front-end personnel does not affect the program logic, which makes the projects that many people work with easy to manage and maintain. Because Smarty has so many advantages, all major Chinese companies adopt this programming method in website programming. For the Smarty manual, visit http://www.smarty.net/docs/en/index.tpl.
The following is a small example of the Smarty program. the function is the same as that in the preliminary article-read data from the t_student in the MySql test database and then display it. The program is divided into five files, respectively, smarty2.php?smarty2.html=smarty2_head.php=smarty2.jsand smarty2.css. In addition, the program must reference the library files of Smarty and JQuery.
1. smarty2_head.php file
// By MoreWindows (http://www.2cto.com)
Define (DB_HOST, 'localhost ');
Define (DB_USER, 'root ');
Define (DB_PASS, '123 ');
Define (DB_DATABASENAME, 'test ');
Define (DB_TABLENAME, 't_ student ');
$ Dbcolarray = array ('id', 'name', 'age ');
?>
// By MoreWindows (http://www.2cto.com)
Define (DB_HOST, 'localhost ');
Define (DB_USER, 'root ');
Define (DB_PASS, '123 ');
Define (DB_DATABASENAME, 'test ');
Define (DB_TABLENAME, 't_ student ');
$ Dbcolarray = array ('id', 'name', 'age ');
?>
2. smarty2.php file
// By MoreWindows (http://www.2cto.com)
Header ("Content-Type: text/html; charset = utf-8 ");
Require ('../smart_libs/Smarty. class. php ');
Require_once ('smarty2 _ head. php ');
Date_default_timezone_set ("PRC ");
// Mysql_connect
$ Conn = mysql_connect (DB_HOST, DB_USER, DB_PASS) or die ("connect failed". mysql_error ());
Mysql_select_db (DB_DATABASENAME, $ conn );
// Number of records in the table
$ SQL = sprintf ("select count (*) from % s", DB_TABLENAME );
$ Result = mysql_query ($ SQL, $ conn );
If ($ result)
{
$ Dbcount = mysql_fetch_row ($ result );
$ Tpl_db_count = $ dbcount [0];
}
Else
{
Die ("query failed ");
}
// Header
$ Tpl_db_coltitle = $ dbcolarray;
// Table content
$ Tpl_db_rows = array ();
$ SQL = sprintf ("select % s from % s", implode (",", $ dbcolarray), DB_TABLENAME );
$ Result = mysql_query ($ SQL, $ conn );
While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC) // equivalent to $ row = mysql_fetch_assoc ($ result)
$ Tpl_db_rows [] = $ row;
Mysql_free_result ($ result );
Mysql_close ($ conn );
$ Tpl = new Smarty;
$ Tpl-> assign ('Db _ count', $ tpl_db_count );
$ Tpl-> assign ('Db _ coltitle', $ tpl_db_coltitle );
$ Tpl-> assign ('Db _ rows ', $ tpl_db_rows );
$ Tpl-> display('smarty2.html ');
?>
// By MoreWindows (http://www.2cto.com)
Header ("Content-Type: text/html; charset = utf-8 ");
Require ('../smart_libs/Smarty. class. php ');
Require_once ('smarty2 _ head. php ');
Date_default_timezone_set ("PRC ");
// Mysql_connect
$ Conn = mysql_connect (DB_HOST, DB_USER, DB_PASS) or die ("connect failed". mysql_error ());
Mysql_select_db (DB_DATABASENAME, $ conn );
// Number of records in the table
$ SQL = sprintf ("select count (*) from % s", DB_TABLENAME );
$ Result = mysql_query ($ SQL, $ conn );
If ($ result)
{
$ Dbcount = mysql_fetch_row ($ result );
$ Tpl_db_count = $ dbcount [0];
}
Else
{
Die ("query failed ");
}
// Header
$ Tpl_db_coltitle = $ dbcolarray;
// Table content
$ Tpl_db_rows = array ();
$ SQL = sprintf ("select % s from % s", implode (",", $ dbcolarray), DB_TABLENAME );
$ Result = mysql_query ($ SQL, $ conn );
While ($ row = mysql_fetch_array ($ result, MYSQL_ASSOC) // equivalent to $ row = mysql_fetch_assoc ($ result)
$ Tpl_db_rows [] = $ row;
Mysql_free_result ($ result );
Mysql_close ($ conn );
$ Tpl = new Smarty;
$ Tpl-> assign ('Db _ count', $ tpl_db_count );
$ Tpl-> assign ('Db _ coltitle', $ tpl_db_coltitle );
$ Tpl-> assign ('Db _ rows ', $ tpl_db_rows );
$ Tpl-> display('smarty2.html ');
?>
3.smarty2.html