2014-09-05
阅读本文之前,推荐先参阅《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文件
<?php
// by MoreWindows( http://www.zhishiwu.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');
?>
<?php
// by MoreWindows( http://www.zhishiwu.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文件
<?php
// by MoreWindows( http://www.zhishiwu.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');
?>
<?php
// by MoreWindows( http://www.zhishiwu.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文件
<!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>表中有{$db_count}条记录</h1>
<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>表中有{$db_count}条记录</h1>
<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文件
$(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
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
6
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
PHP中include和require区别之我见
09-05
常用dos命令及语法
2014-09-27
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
java中的info是什么意思
2022-03-24
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
PHP数组函数array
2014-09-05
学ug编程如何快速入门?
2022-03-17
斗罗大陆魂师对决手游官方版下载v2.31.1 安卓版
卡牌对战 1.55G
下载坦克大决战游戏下载v1.9462 安卓官方正版
射击枪战 388.5M
下载孤胆车神维加斯官方正版(Gangstar Vegas)下载v8.5.1c 安卓手机版
射击枪战 2.44G
下载孤胆车神维加斯僵尸城版本下载v8.5.1c 安卓版
射击枪战 2.44G
下载英雄联盟云顶之弈手机版本下载v14.8.5768838 安卓最新版本
策略塔防 78.2M
下载苍蓝前线手游下载v1.1.0 安卓版
策略塔防 464.3M
下载崩坏学园2手游下载v12.2.8 安卓最新版本
角色扮演 323.3M
下载彩虹联萌手游下载v7.3.23015 安卓官方版
卡牌对战 804.7M
下载花花填色苹果版下载v2.2.5 iPhone版
下载
奥特曼超时空英雄官方正版下载v1.0.2 安卓版
下载
全民奇迹2ios版下载v15.0.0 iphone版
下载
罗布乐思roblox国际服最新版官方正版下载v2.682.538 安卓版
下载
com.roblox.client国际服下载v2.682.538 安卓版
下载
绝地鸭卫游戏下载v0.2.3 安卓版
下载
梦幻模拟战官服下载v6.13.2110 安卓最新版
下载
天龙八部手游8868版下载v1.133.2.2 安卓版
下载