搜尋
首頁php教程php手册PHP访问MySql数据库 中级篇 Smarty技术

PHP访问MySql数据库 中级篇 Smarty技术

Jun 13, 2016 am 10:46 AM
mysqlphpsmarty中級科技推薦資料庫訪問閱讀

阅读本文之前,推荐先参阅《PHP访问MySql数据库 初级篇》。

Smarty是一个使用PHP语言写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,将原本与HTML代码混杂在一起PHP代码进行了分离。从而使PHP程序员同网站的前端程序员可以达到良好的分工——PHP程序员改变程序的逻辑内容不会影响到前端人员的页面设计,前端人员重新修改页面的样式也不会影响到程序的程序逻辑,这使得多人合作的项目变得尤为轻松和易于管理维护。正因为Smarty有这么多的优点,所以国内各大公司在网站编程时均采用这种编程方法。Smarty的手册可以访问http://www.smarty.net/docs/en/index.tpl。

 

下面是Smarty程序的一个小范例,功能上与初级篇相同——从MySql的test数据库中的t_student读取数据然后显示。程序共分为5个文件,分别为smarty2.php、smarty2.html、smarty2_head.php、smarty2.js和smarty2.css。此外程序要引用Smarty和JQuery的库文件。

1.smarty2_head.php文件

// 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文件

// 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); 
 
//表中记录条数  
$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"); 

 
//表头  
$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'); 
?> 
// 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);

//表中记录条数
$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");
}

//表头
$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; 
}
程序运行结果如下:

 



 

上例展示了Smarty的基本用法,当然Smarty还提供了更加方便使用的接口函数,如对表格,可以使用{html_table}来快速生成表格。有兴趣的读者可以试试。

现在这个程序再加上《jquery 表格的增加删除和修改及设置奇偶行颜色》中对表格的增加删除和修改功能就基本上可以完善了,请参见下一篇《PHP访问MySql数据库 高级篇 AJAX技术》。

 摘自 MoreWindows

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器