Share a case of PHP paging function code, using this function to implement the pagination code is very good.
Code, PHP paging function.
<?php
/*
* Created on 2011-07-28
* Author:lkk, Http://lianq.net
* Use method:
Require_once (' mypage.php ');
$result =mysql_query ("SELECT * FROM MyTable", $myconn);
$total =mysql_num_rows ($result); Total information obtained
Pagedivide ($total, 10); To call a paging function
Database operations
$result =mysql_query ("select * FROM MyTable limit $sqlfirst, $shownu", $myconn);
while ($row =mysql_fetch_array ($result)) {
... Your actions
}
Echo $pagecon; Output Paging navigation content
*/
if (!function_exists ("Pagedivide")) {
# $total Total Information
# $SHOWNU Display quantity, default 20
# $url this page link
function Pagedivide ($total, $shownu =20, $url = ') {
# $page Current page number
# $sqlfirst MySQL Database start entry
# $pagecon Pagination Navigation content
Global $page, $sqlfirst, $pagecon, $_server;
$GLOBALS ["Shownu"]= $shownu;
if (Isset ($_get[' page ')) {
$page =$_get[' page '];
}else $page = 1;
#如果 $url Use the default, or null value, the assignment is the URL to this page
if (! $url) {$url =$_server["Request_uri"];}
#URL分析
$parse _url=parse_url ($url);
@ $url _query= $parse _url["Query"]; Take out the question mark? After the content
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); Last page, total pages
$page =min ($LASTPG, $page);
$PREPG = $page-1; Previous page
$NEXTPG = ($page = = $LASTPG 0: $page + 1); Next page
$sqlfirst = ($page-1) * $SHOWNU;
#开始分页导航内容
$pagecon = "Show First". ($total? ($sqlfirst + 1): 0). " -". Min ($sqlfirst + $shownu, $total)." Records, total <B> $total </B> Records ";
if ($lastpg <=1) return false; If only one page jumps out
if ($page!=1) $pagecon. = "<a href= ' $url =1 ' > Home </a>"; else $pagecon. = "Home page";
if ($PREPG) $pagecon. = "<a href= ' $url = $prepg ' > front page </a>"; else $pagecon. = "front page";
if ($NEXTPG) $pagecon. = "<a href= ' $url = $nextpg ' > Back page </a>"; else $pagecon. = "Back Page";
if ($page!= $lastpg) $pagecon. = "<a href= ' $url = $lastpg ' > Last </a>"; else $pagecon. = "Last";
#下拉跳转列表, looping through all page numbers
$pagecon. = "to <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> page, total $lastpg page";
}
}else die (' Pagedivide () has a function of the same name already exists! ');
?>