smarty分页的例子
配置页面 const.php:
<?
@session_start();
require_once("smarty.php");//加载smarty模板类
require_once("adodb/adodb.inc.php");//加载adodb数据库
//连接数据库
$dbhost = 'localhost'; // 数据库服务器
$dbuser = 'root'; // 数据库用户名
$dbpwd= 'root'; // 数据库密码
$dbname='hejia'; //数据库名称
$conn = NewAdoConnection('mysql'); // 建立连接对象
$conn->Connect($dbhost, $dbuser, $dbpwd, $dbname); //连接数据库
$conn->Query("Set Names 'gb2312'");//来源于mysql_query("SET NAMES GBK");
date_default_timezone_set(PRC); //PHP获取时间差8小时的解决办法,也可在php.ini里修改date.timezone为PRC
?>
首页 index.php:
<?
require_once("const.php");
$sql1="select id,hits,title,add_date from oa_art order by id desc"; //SQL语句
$pageSize=4; //设置每页记录数
$sql=$sql1." limit ".($pageSize * ((empty($_REQUEST['page']) ? 1 : $_REQUEST['page'])-1)).", ".$pageSize;
$news_array=$conn->getall($sql);
$news_array1=$conn->getall($sql1);
$page_url="index.php"; //页面url地址
$totalnumber=count($news_array1); //获得总记录数
$midPage=5; //数字导航链接数
page();// 调用分页函数
for ($i = 0; $i <=ceil($totalnumber/$pageSize); $i++) $page_option[] = $i;//产生分页下拉列表数组
$smarty->assign("page_option",$page_option);
$smarty->assign("news_data",$news_array);
$smarty->assign("mytitle","企业网站首页");
$smarty->display("tpl.htm",$page);
?>
分页函数:
<?php
function page()
{
global $smarty,$start,$page_url,$pageSize,$midPage,$totalnumber;
$total = $totalnumber ; //获得总记录数
$totalPage = ceil($total/$pageSize); //获得总页数
$currentPage=@$_REQUEST['page']+0; //当前页
if(!is_numeric($currentPage) || $currentPage < 1 || empty($currentPage) || $currentPage > $totalPage)
$currentPage=1; //初始化当前页
$url = preg_replace(array("!(([&]|^)(page)[=]?([^&]+)?)|((([&]){2,})|(^[&])|([&]$))!",),array(""),$_SERVER["QUERY_STRING"]); // 设置地址,正则替换掉
$url.=($url?"&":"").'page'; // 追加
$start = ($currentPage-1)*$pageSize;
$back = $currentPage > 1?"<a href=\"?$url=".($currentPage-1)."\" Title='上一页'><u><<</u></a>\n":"";
$next = $currentPage < $totalPage ?"<a href=\"?$url=".($currentPage+1)."\" Title='下一页'><u>>></u></a>\n":"";
$first = $currentPage > 1?"<a href=\"?$url=1\"><u>首页</u></a>\n":"";
$last = $currentPage < $totalPage?"<a href=\"?$url=$totalPage\"><u>尾页</u></a>\n":"";
// 导航链接
$midPages = '';
$num = $currentPage-floor($midPage/2);
if($num > 0)
{
if(($totalPage-$num) < $midPage)
{
$tmp = $totalPage - $midPage;
$num = $tmp< 0 ? 1 : ++$tmp;
}
}else $num = 1;
for($i=1; $i<=$midPage;$i++,$num++)
{
if($num > $totalPage) break;
$midPages .= ($num == $currentPage) ? '['.$num.'] ' : "<a href='?".$url."=".$num."'><u>".$num."</u></a> ";
}
$smarty->assign("page_total",$total); // 总计
$smarty->assign("page_currentPage",$currentPage); // 当前页码
$smarty->assign("page_totalPage",$totalPage); // 总页数
$smarty->assign("page_back",$back); // 上一页
$smarty->assign("page_next",$next); // 下一页
$smarty->assign("page_first",$first); // 首页
$smarty->assign("page_last",$last); // 尾页
$smarty->assign("page_midPages",$midPages); // 中间页
$smarty->assign("page_url",$page_url); // 当前页地址
}
?>
模板页 tpl.htm:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><{$mytitle}></title>
<link href="index.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="66%" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCCCCC">
<tr align="center">
<td bgcolor="#FFFFFF">标题</td>
<td bgcolor="#FFFFFF">发布日期</td>
<td bgcolor="#FFFFFF">点击次数</td>
</tr>
<{section name=nloop loop=$news_data}>
<tr align="center" bgcolor="<{cycle values="#f1f2f3,#ffffff"}>">
<td><a href="show<{$news_data[nloop].id}>.htm"><{$news_data[nloop].title}></a></td>
<td><{$news_data[nloop].add_date|date_format:"%Y-%m-%d"}></td>
<td><{$news_data[nloop].hits}></td>
</tr>
<{sectionelse}>
<tr><td colspan="3">对不起,没有任何新闻输入!</td></tr>
<{/section}>
<tr><td colspan="3">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<form method="get" onSubmit="location.href='?<{$page_url}>='">
<tr>
<td align="right">
共<{$page_total}>条
第<{$page_currentPage}>/<{$page_totalPage}>页
<{$page_first}>
<{$page_back}>
<{$page_midPages}>
<{$page_next}>
<{$page_last}>
<input name="page" type="text" size="1" style="font-size:9pt;" value="<{$page_currentPage}>" id='page'>
<input type="submit" value="GO" style="font-size:9pt;">
转到
<select onChange="javascript:location.href='<{$page_url}>?page='+this.options[this.selectedIndex].value;">
<{html_options options=$page_option selected=$page_currentPage}>
</select>
</td>
</tr>
</form>
</table>
</td></tr>
</table>
<{include file="bottom.htm"}>
</body>
</html>
您可能感兴趣的文章:
smarty获得当前url示例代码
PHP模板引擎Smarty缓存使用
PHP中使用smarty生成静态文件的例子
smarty 模板if else使用实例与入门教程
smarty模板中get、post、request、cookies、session变量用法
smarty获取当前日期时间和格式化日期时间的实例
有关smarty的基本设置
smarty中使用php函数的方法
在php中配置使用smarty模板引擎
php模版生成html的小例子