Home > Backend Development > PHP Tutorial > PHP Access MySql Database Intermediate Smarty Technology_PHP Tutorial

PHP Access MySql Database Intermediate Smarty Technology_PHP Tutorial

WBOY
Release: 2016-07-13 17:47:52
Original
909 people have browsed it

Before reading this article, it is recommended to read "Accessing MySql Database with PHP - Elementary".

Smarty is a template engine written in PHP language and is currently one of the most famous PHP template engines in the industry. It separates the logic code and external content, and separates the PHP code that was originally mixed with the HTML code. This allows PHP programmers to achieve a good division of labor with the website's front-end programmers - PHP programmers' changes to the logical content of the program will not affect the front-end staff's page design, and front-end staff's re-modification of the page style will not affect the program's performance. Program logic, which makes multi-person collaboration projects particularly easy and easy to manage and maintain. Because Smarty has so many advantages, major domestic companies adopt this programming method when programming their websites. Smarty's manual can be accessed at http://www.smarty.net/docs/en/index.tpl.

The following is a small example of the Smarty program, which is functionally the same as the primary chapter - reading data from t_student in the MySql test database and then displaying it. The program is divided into 5 files, namely smarty2.php, smarty2.html, smarty2_head.php, smarty2.js and smarty2.css. In addition, the program must reference the Smarty and JQuery library files.

1. smarty2_head.php file

// by MoreWindows( http://www.BkJia.com )


define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '111111');
define(DB_DATABASENAME, 'test');
define(DB_TABLENAME, 't_student');

$dbcolarray = array('id', 'name', 'age');
?>
// by MoreWindows( http://www.BkJia.com )

define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '111111');
define(DB_DATABASENAME, 'test');
define(DB_TABLENAME, 't_student');

$dbcolarray = array('id', 'name', 'age');
?>
2. smarty2.php file

// 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;

//Contents in the table
$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.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;

//表中的内容
$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))//与$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文件

 
 
 
 
 
 
{$smarty.const.DB_TABLENAME} 

 
 

表中有{$db_count}条记录

 
 
{foreach $db_coltitle as $col} 
     
{/foreach} 
{foreach $db_rows as $dbrow} 
     
    {foreach $dbrow as $k=>$val} 
         
    {/foreach} 
     
{/foreach} 
{$col}
{$val}
 
 
 






{$smarty.const.DB_TABLENAME}


表中有{$db_count}条记录



{foreach $db_coltitle as $col}
   
{/foreach}
{foreach $db_rows as $dbrow}
   
    {foreach $dbrow as $k=>$val}
       
    {/foreach}
   
{/foreach}
{$col}
{$val}



4.smarty2.js文件

$(document).ready(function() 

    //用CSS控制奇偶行的颜色  
    $("table tr:odd").css("background-color", "#e6e6fa"); 
    $("table tr:even").css("background-color", "#fff0fa"); 
});   
$(document).ready(function()
{
    //用CSS控制奇偶行的颜色
    $("table tr:odd").css("background-color", "#e6e6fa");
    $("table tr:even").css("background-color", "#fff0fa");
}); 
5.smarty2.css文件

@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 results of running the program are as follows:



The above example shows the basic usage of Smarty. Of course, Smarty also provides more convenient interface functions. For example, for tables, you can use {html_table} to quickly generate tables. Interested readers can try it.

Now this program can be basically completed by adding the functions of adding, deleting, modifying and setting odd-even row colors in jquery tables. Please refer to the next article "Advanced AJAX Technology for Accessing MySql Database with PHP" 》.

Excerpted from MoreWindows

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478476.htmlTechArticleBefore reading this article, it is recommended to read "Primary Chapter of PHP Accessing MySql Database". Smarty is a template engine written in PHP language. It is one of the most famous PHP template engines in the industry...
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