php分頁顯示函數 源代碼
<!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>google分頁</title>
<meta name="keywords" content="仿google php分頁程序 " />
<meta name="description" content="仿google php分頁程序" />
<style type="text/css">
<!--
body { font-family: Arial, Helvetica, sans-serif; font-size: 12px; margin: 0px; padding: 0px; }
div { height: auto; width: 800px; margin: 10px auto; line-height:20px; }
/*CSS manu style pagination*/
.manu {
PADDING-RIGHT: 3px; PADDING-LEFT: 3px; PADDING-BOTTOM: 3px; MARGIN: 3px; PADDING-TOP: 3px; TEXT-ALIGN: center
}
.manu A {
BORDER-RIGHT: #eee 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #eee 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #eee 1px solid; COLOR: #036cb4; PADDING-TOP: 2px; BORDER-BOTTOM: #eee 1px solid; TEXT-DECORATION: none
}
.manu A:hover {
BORDER-RIGHT: #999 1px solid; BORDER-TOP: #999 1px solid; BORDER-LEFT: #999 1px solid; COLOR: #666; BORDER-BOTTOM: #999 1px solid
}
.manu A:active {
BORDER-RIGHT: #999 1px solid; BORDER-TOP: #999 1px solid; BORDER-LEFT: #999 1px solid; COLOR: #666; BORDER-BOTTOM: #999 1px solid
}
.manu .current {
BORDER-RIGHT: #036cb4 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #036cb4 1px solid; PADDING-LEFT: 5px; FONT-WEIGHT: bold; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #036cb4 1px solid; COLOR: #fff; PADDING-TOP: 2px; BORDER-BOTTOM: #036cb4 1px solid; BACKGROUND-COLOR: #036cb4
}
.manu .disabled {
BORDER-RIGHT: #eee 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #eee 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 2px; MARGIN: 2px; BORDER-LEFT: #eee 1px solid; COLOR: #ddd; PADDING-TOP: 2px; BORDER-BOTTOM: #eee 1px solid
}
-->
</style>
</head>
<body>
<?php
require('web_page.php'); //包含分頁程序
//數據庫配置
$mysql_host = 'localhost'; //數據庫服務器
$mysql_user = 'root'; //數據庫用戶名
$mysql_pass = '123456'; //數據庫密碼
$mysql_db = 'test'; //數據庫名
//連接mysql數據庫
$link = mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die ('連接MYSQL服務器出錯');
mysql_select_db($mysql_db,$link) or die ('連接MYSQL數據庫出錯');
//分頁開始
$sql_page = "select news_id,news_title from cms_news order by news_id desc";
$sql = mysql_query($sql_page);
$num = mysql_num_rows($sql); //總條數
$max = 1; //每頁條數
$pagenum = ceil($num/$max); //可分頁數
if(!isset($_GET['page']) or !intval($_GET['page']) or !is_numeric($_GET['page']) or $_GET['page'] > $pagenum){
$page = 1; //當頁數不存在 不為十進制數 不是數字 大於可分頁數 為1
}else{
$page = $_GET['page']; //當前頁數
}
$min = ($page-1)*$max; //當前頁從$min條開始
$sql = "{$sql_page} limit $min,$max";
echo '<div>';
if($num){
$sql = mysql_query($sql);
for(;$row = mysql_fetch_array($sql);){
echo '<li><a href="show.php?id='.$row['news_id'].'">'.$row['news_title'].'</a></li>';
}
}else{
echo '<li>暫無</li>';
}
echo '</div>';
//mysql_free_result($sql); //釋放資源
?>
<div class="manu"><?php web_page(""); //調用輸出分頁,引用你的頁面其他參數 如:web_page("&class=2&news=6") ?></div>
<?php
//分頁結束
mysql_close();
?>
</body>
</html>
上面為調用方法下面為分頁程序
<?php
function web_page($pageurl="", $pageselect = true){
global $page,$num,$pagenum; //當前頁數 總頁數 可分頁數
echo "共 $num 條記錄,";
$uppage = $page - 1; //上一頁
$downpage = $page + 1; //下一頁
$lr = 5; //顯示多少個頁數連接
$left = floor(($lr-1)/2); //左顯示多少個頁數連接
$right = floor($lr/2); //右顯示多少個頁數連接
//下面求開始頁和結束頁
if($page <= $left){ //如果當前頁左不足以顯示頁數
$leftpage = 1;
$rightpage = (($lr<$pagenum)?$lr:$pagenum);
}elseif(($pagenum-$page) < $right){ //如果當前頁右不足以顯示頁數
$leftpage = (($pagenum<$lr)?1:($pagenum-$lr+1));
$rightpage = $pagenum;
}else{ //左右可以顯示頁數
$leftpage = $page - $left;
$rightpage = $page + $right;
}
//前$lr頁和後$lr頁
$qianpage = (($page-$lr) < 1?1:($page-$lr));
$houpage = (($page+$lr) > $pagenum?$pagenum:($page+$lr));
//輸出分頁
if($page != 1){
echo "<a title="首頁" href="".$_SERVER['PHP_SELF']."?$pageurl"><<</a> <a title="上一頁" href="".$_SERVER['PHP_SELF']."?page=$uppage$pageurl"><</a> ";
}else{
echo "<span class='disabled'><<</span><span class='disabled'><</span> ";
}
for($pages = $leftpage; $pages <= $rightpage; $pages++){
if($pages == $page){
echo "<span class='current'>$pages</span> ";
}else{
echo "<a href="?page=$pages$pageurl">$pages</a> ";
}
}
if($page != $pagenum){
echo "<a title="下一頁" href="".$_SERVER['PHP_SELF']."?page=$downpage$pageurl">></a> <a title="末頁" href="".$_SERVER['PHP_SELF']."?page=$pagenum$pageurl">>></a>";
}else{
echo "<span class='disabled'>></span><span class='disabled'> >></span> ";
}
//跳轉
$javapage = <<<EOM
<script language="javascript">
function web_page(targ,selObj,restore){
eval("self"+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
EOM;
echo $javapage;
if ($pageselect){
echo "跳轉至 <select onchange="web_page('parent',this,0)" name="menu1">";
for($pages = 1; $pages <= $pagenum; $pages++){
$selected = ($pages == $page)?" selected="selected"":"";
echo "<option value="".$_SERVER['PHP_SELF']."?page=$pages$pageurl"$selected> $pages</option>";
}
echo "</select> 頁";
}
}
?>