第一個smarty例子--分頁顯示數據
阿新 • • 發佈:2018-04-27
nbsp oca AS com tr1 xhtml 建立 xmlns set 模板頁index.tpl:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>查看留言</title>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
</head> <body>
<{*assign var="login" value=0*}>
<div align="center">
<p><a href="index.php">主頁</a> <a href="addmeg.php">留言</a>
<{if $login==0}> <a href="gli.php">管理</a><{/if}> <{*login=0未登陸,顯示管理鏈接*}>
</p>
</div>
<{section name=lp loop=$ly}>
<table width="590" border="1" align="center" cellpadding="0" cellspacing="1">
<tr>
<td width="85" rowspan="2"><p>留言人:<br />
<{$ly[lp].fbr}></p>
</td>
<td width="427" height="23"><p> 標題:<{$ly[lp].tm}></p>
</td>
</tr>
<tr>
<td height="58"> 內容:<{$ly[lp].nr}></td>
</tr>
</table><br />
<{/section}>
<p align="center">總<span class="STYLE1"><{$pcunt}></span>頁 當前為第<span class="STYLE1"><{$page}></span>頁 <a href="index.php">首頁</a> <{$qian}><{$next}><a href="index.php?page=<{$pcunt}>">最後一頁</a></p>
</body>
</html> ////////////////////////////////////////////////////////////////////////////// index.php: <?php
/*********************************************
*
* 文件名:index.php
* 作 用: 顯示留言分頁
* 作 者: 龍的心
* Q Q:282129207
*
*********************************************/
require("./class/Smarty.class.php"); //包含smarty類文件 $smarty = new Smarty(); //建立smarty實例對象$smarty
$smarty->template_dir = ‘./templates/‘;
$smarty->compile_dir = ‘./templates_c/‘;
$smarty->config_dir = ‘./configs/‘;
$smarty->cache_dir = ‘./cache/‘;
$smarty->caching = false; //這裏是調試時設為false,發布時請使用true $smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
mysql_connect(‘localhost‘,‘root‘,‘root‘);
mysql_select_db(‘nihao‘);
mysql_query("set names ‘gb2312‘"); $page=$_GET[‘page‘];
if($page==null)
$page=1;
$psize=4; //每頁記錄數
$str="select *from ly";
$query=mysql_query($str);
$num=@mysql_num_rows($query);// 總記錄數
$pcunt=ceil($num/$psize);//總頁數
$nextpage = $page+1;
$qianpage= $page-1;
$start=($page-1)*$psize;
$str="select *from ly limit $start,$psize";
$query=mysql_query($str);
while($arr=mysql_fetch_array($query))
{//print_r($arr);
$array[]=$arr;
} if($page>1) $str1="<a href=index.php?page=$qianpage>上一頁</a> ";
if($page<$pcunt) $str2="<a href=index.php?page=$nextpage>下一頁</a> ";
$smarty->assign("login", "0"); //login=0未登陸,顯示管理鏈接
$smarty->assign("ly", $array);
$smarty->assign("page", $page);
$smarty->assign("qian", $str1);
$smarty->assign("next", $str2);
$smarty->assign("pcunt", $pcunt); $smarty->display("index.tpl"); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>查看留言</title>
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
-->
</style>
</head> <body>
<{*assign var="login" value=0*}>
<div align="center">
<p><a href="index.php">主頁</a> <a href="addmeg.php">留言</a>
<{if $login==0}> <a href="gli.php">管理</a><{/if}> <{*login=0未登陸,顯示管理鏈接*}>
</p>
</div>
<{section name=lp loop=$ly}>
<table width="590" border="1" align="center" cellpadding="0" cellspacing="1">
<tr>
<td width="85" rowspan="2"><p>留言人:<br />
<{$ly[lp].fbr}></p>
</td>
<td width="427" height="23"><p> 標題:<{$ly[lp].tm}></p>
</td>
</tr>
<tr>
<td height="58"> 內容:<{$ly[lp].nr}></td>
</tr>
</table><br />
<{/section}>
<p align="center">總<span class="STYLE1"><{$pcunt}></span>頁 當前為第<span class="STYLE1"><{$page}></span>頁 <a href="index.php">首頁</a> <{$qian}><{$next}><a href="index.php?page=<{$pcunt}>">最後一頁</a></p>
</body>
</html> ////////////////////////////////////////////////////////////////////////////// index.php: <?php
/*********************************************
*
* 文件名:index.php
* 作 用: 顯示留言分頁
* 作 者: 龍的心
* Q Q:282129207
*
*********************************************/
require("./class/Smarty.class.php"); //包含smarty類文件 $smarty = new Smarty(); //建立smarty實例對象$smarty
$smarty->template_dir = ‘./templates/‘;
$smarty->compile_dir = ‘./templates_c/‘;
$smarty->config_dir = ‘./configs/‘;
$smarty->cache_dir = ‘./cache/‘;
$smarty->caching = false; //這裏是調試時設為false,發布時請使用true $smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
mysql_connect(‘localhost‘,‘root‘,‘root‘);
mysql_select_db(‘nihao‘);
mysql_query("set names ‘gb2312‘"); $page=$_GET[‘page‘];
if($page==null)
$page=1;
$psize=4; //每頁記錄數
$str="select *from ly";
$query=mysql_query($str);
$num=@mysql_num_rows($query);//
$pcunt=ceil($num/$psize);//總頁數
$nextpage = $page+1;
$qianpage= $page-1;
$start=($page-1)*$psize;
$str="select *from ly limit $start,$psize";
$query=mysql_query($str);
while($arr=mysql_fetch_array($query))
{//print_r($arr);
$array[]=$arr;
} if($page>1) $str1="<a href=index.php?page=$qianpage>上一頁</a> ";
if($page<$pcunt) $str2="<a href=index.php?page=$nextpage>下一頁</a> ";
$smarty->assign("login", "0"); //login=0未登陸,顯示管理鏈接
$smarty->assign("ly", $array);
$smarty->assign("page", $page);
$smarty->assign("qian", $str1);
$smarty->assign("next", $str2);
$smarty->assign("pcunt", $pcunt); $smarty->display("index.tpl"); ?>
第一個smarty例子--分頁顯示數據