配置页面 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: