php分頁函數範例程式碼分享_php執行個體

來源:互聯網
上載者:User

分享一例php分頁函數代碼,用此函數實現分頁代碼很不錯。

代碼,php分頁函數。

複製代碼 代碼如下:

<?php
/*
* Created on 2011-07-28
* Author : LKK , http://lianq.net
* 使用方法:
require_once('mypage.php');
$result=mysql_query("select * from mytable", $myconn);
$total=mysql_num_rows($result);    //取得資訊總數
pageDivide($total,10);     //調用分頁函數

//資料庫操作
$result=mysql_query("select * from mytable limit $sqlfirst,$shownu", $myconn);
while($row=mysql_fetch_array($result)){
...您的操作
}
echo $pagecon;    //輸出分頁導航內容
*/

if(!function_exists("pageDivide")){
#$total     資訊總數
#$shownu    顯示數量,預設20
#$url     本頁連結
function pageDivide($total,$shownu=20,$url=''){

#$page 當前頁碼
#$sqlfirst mysql資料庫起始項
#$pagecon    分頁導航內容
global $page,$sqlfirst,$pagecon,$_SERVER;
$GLOBALS["shownu"]=$shownu;

if(isset($_GET['page'])){
$page=$_GET['page'];
}else $page=1;

#如果$url使用預設,即空值,則賦值為本頁URL
if(!$url){ $url=$_SERVER["REQUEST_URI"];}

#URL分析
$parse_url=parse_url($url);
@$url_query=$parse_url["query"];    //取出在問號?之後內容
if($url_query){
$url_query=preg_replace("/(&?)(page=$page)/","",$url_query);
$url = str_replace($parse_url["query"],$url_query,$url);
if($url_query){
$url .= "&page";
}else $url .= "page";
}else $url .= "?page";

#頁碼計算
$lastpg=ceil($total/$shownu);    //最後頁,總頁數
$page=min($lastpg,$page);
$prepg=$page-1; //上一頁
$nextpg=($page==$lastpg ? 0 : $page+1); //下一頁
$sqlfirst=($page-1)*$shownu;

#開始分頁導航內容
$pagecon = "顯示第 ".($total?($sqlfirst+1):0)."-".min($sqlfirst+$shownu,$total)." 條記錄,共 <B>$total</B> 條記錄";
if($lastpg<=1) return false;    //如果只有一頁則跳出

if($page!=1) $pagecon .=" <a href='$url=1'>首頁</a> "; else $pagecon .=" 首頁 ";
if($prepg) $pagecon .=" <a href='$url=$prepg'>前頁</a> "; else $pagecon .=" 前頁 ";
if($nextpg) $pagecon .=" <a href='$url=$nextpg'>後頁</a> "; else $pagecon .=" 後頁 ";
if($page!=$lastpg) $pagecon.=" <a href='$url=$lastpg'>尾頁</a> "; else $pagecon .=" 尾頁 ";

#下拉捷徑清單,迴圈列出所有頁碼
$pagecon .=" 到第 <select name='topage' size='1' onchange='window.location=\"$url=\"+this.value'>\n";
for($i=1;$i<=$lastpg;$i++){
if($i==$page) $pagecon .="<option value='$i' selected>$i</option>\n";
else $pagecon .="<option value='$i'>$i</option>\n";
}
$pagecon .="</select> 頁,共 $lastpg 頁";

}
}else die('pageDivide()同名函數已經存在!');
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.