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
<? Php
// By MoreWindows (http://www.bkjia.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 ');
?>
<? Php
// By MoreWindows (http://www.bkjia.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
<? Php
// By MoreWindows (http://www.bkjia.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 ');
?>
<? Php
// By MoreWindows (http://www.bkjia.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
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Link href = "smarty2.css" rel = "stylesheet" type = "text/css" media = "all"/>
<Script type = "text/javascript" src = "../jquery-1.7.min.js"> </script>
<Script type = "text/javascript" src = "smarty2.js"> </script>
<Title >{$ smarty. const. DB_TABLENAME} </title>
</Head>
<Body>
<H1> The table contains {$ db_count} records <Table border = "1" align = "center" cellpadding = "10" cellspacing = "2" bordercolor = "# ffaaoo">
{Foreach $ db_coltitle as $ col}
<Th >{$ col }</th>
{/Foreach}
{Foreach $ db_rows as $ dbrow}
<Tr>
{Foreach $ dbrow as $ k => $ val}
<Td >{$ val} </td>
{/Foreach}
</Tr>
{/Foreach}
</Table>
</Body>
</Html>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Link href = "smarty2.css" rel = "stylesheet" type = "text/css" media = "all"/>
<Script type = "text/javascript" src = "../jquery-1.7.min.js"> </script>
<Script type = "text/javascript" src = "smarty2.js"> </script>
<Title >{$ smarty. const. DB_TABLENAME} </title>
</Head>
<Body>
<H1> The table contains {$ db_count} records <Table border = "1" align = "center" cellpadding = "10" cellspacing = "2" bordercolor = "# ffaaoo">
{Foreach $ db_coltitle as $ col}
<Th >{$ col }</th>
{/Foreach}
{Foreach $ db_rows as $ dbrow}
<Tr>
{Foreach $ dbrow as $ k => $ val}
<Td >{$ val} </td>
{/Foreach}
</Tr>
{/Foreach}
</Table>
</Body>
</Html>
4. smarty2.js File
$ (Document). ready (function ()
{
// Use CSS to control the color of the even row
$ ("Table tr: odd" ).css ("background-color", "# e6e6fa ");
$ ("Table tr: even" ).css ("background-color", "# fff0fa ");
});
$ (Document). ready (function ()
{
// Use CSS to control the color of the even row
$ ("Table tr: odd" ).css ("background-color", "# e6e6fa ");
$ ("Table tr: even" ).css ("background-color", "# fff0fa ");
});
5.smarty2.css File
@ Charset "UTF-8 ";
H1
{
Color: Red;
Text-align: center;
}
Table th
{
Background-color: #7cfc00;
}
@ Charset "UTF-8 ";
H1
{
Color: Red;
Text-align: center;
}
Table th
{
Background-color: #7cfc00;
}
The program running result is as follows:
The previous example shows the basic usage of Smarty. Of course, Smarty also provides more convenient interface functions. For example, you can use {html_table} to quickly generate a table. If you are interested, try it.
Now, in addition to the "add Delete and modify jquery table and set the color of the parity row" function, the function of adding Delete and modify tables is basically improved, see the next article "Advanced AJAX technology for accessing MySql databases using PHP".
From MoreWindows