看到许多人在发SMARTY 的分页,但总觉得不够简单。下面,我将以例子来说明这分页的使用。绝对的简单好用,显示的样式也多呀~
(源码文件一会打包)
补:效果图(图1)
以下是模版文件page.tpl.htm
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" />
<link href="css/home.css" rel="stylesheet" type="text/css" />
<link href="read.css" rel="stylesheet" type="text/css" />
<title>推荐导读</title>
</head>
<body>
<div class="row">
<div class="tab_label" style="background-color:#FEF4F5; float:left">
<div style="float:left; width:420px; overflow:hidden">
<ul>
{foreach from=$list item=rs key=key}
<li>
<span class="li_dot">4</span>
<a target="_blank" href="#{$rs.Title}">{$rs.Title}</a>
</li>
{/foreach}
</ul>
</div>
</div>
</div>
<p>
{pager rowcount=$totalpage limit=1 shift=1 no_first=true separator="|" class_text="pager_text" txt_pos="side"}
</body>
</html>
好了,让我们看看这一段:
{pager rowcount=$totalpage limit=1 shift=1 no_first=true separator="|" class_text="pager_text" txt_pos="side"}
这一段是主要的,也就是导航部分
separator="|" 就是页码之间的一个间隔
rowcount=$totalpage 总记录数
limit=1 页码间隔加1 假如设为2 可能会变成 1 3 5 7 .....
其它的也不重要吧,呵呵~,参数的具体设置可以查看function.pager.php (老外的。。。半懂半不懂)
那么我们的程序怎样写呢?
<?php
require_once("./includes/init.php");
/* SMARTY 分页 */
//$smarty->caching = false;
$lady_url ="http://lady.poco.cn/";
$content = array();
$pagesize = 5;
$sql.= "SELECT Image,Title,Intro,CreationDate,ContentID ".
"FROM cms_db.cms_content_4 ".
"WHERE Zine_type_id IN(5,6,7,11,24) ".
"ORDER BY ContentID DESC ";
$res = $db->query($sql);
//结果集
$total = $res->RecordCount();
//记录条数
$totalpage = ceil($total/$pagesize);
//总页数
$page = ceil($_GET["pos"])+1;
//当前页
$sql.= "limit ".($pagesize * ($page-1)).", ".$pagesize;
//语句
$idx = 0;
$res = $db->query($sql);
while ($rs = $res->FetchRow())
{
$content[$idx]['Image'] = $rs['Image'];
$content[$idx]['Title'] = $rs['Title'];
$content[$idx]['Intro'] = $rs['Intro'];
$content[$idx]['html_url'] = date("Y-m-d",$rs['CreationDate'])."/".$rs['ContentID'].".html";
$content[$idx]['ContentID'] = $rs['ContentID'];
$idx++;
}
$smarty->assign('totalpage',$totalpage);
$smarty->assign('list',$content);
$smarty->assign('lady_url',$lady_url);
$smarty->display("page.tpl.htm",$page);
?>
我们只要传给模版 $smarty->assign('totalpage',$totalpage); 就可以了。重要的部分我都有注解,应该明白吧!
$smarty->display("page.tpl.htm",$page); 目的是为了让它每一个页都生成一个新的页。。不然,刷来刷去都是同一个页。
最后,只要把function.pager.php 放到smarty\plugins 目录下,你就可调用分页了。
怎样?够简单了吧~打包程序给你们
看了要回贴啊,动力所在~