第一个smarty例子--分页显示数据

模板页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"); 
?>  

猜你喜欢

转载自www.cnblogs.com/apolloren/p/8955357.html