php paging function sample code to share
- 2021-01-06 00:30:52
- OfStack
Share an example of php paging function code, using this function to implement paging code is good.
Code, php paging function.
<?php
/*
* Created on 2011-07-28
* Author : LKK , http://lianq.net
* Usage:
require_once('mypage.php');
$result=mysql_query("select * from mytable", $myconn);
$total=mysql_num_rows($result); // Total amount of information obtained
pageDivide($total,10); // Call the paging function
// Database operation
$result=mysql_query("select * from mytable limit $sqlfirst,$shownu", $myconn);
while($row=mysql_fetch_array($result)){
... Your operating
}
echo $pagecon; // Output paging content
*/
if(!function_exists("pageDivide")){
#$total The total number of information
#$shownu According to the number of , The default 20
#$url Links on this page
function pageDivide($total,$shownu=20,$url=''){
#$page The current page number
#$sqlfirst mysql Database start entry
#$pagecon Paging content
global $page,$sqlfirst,$pagecon,$_SERVER;
$GLOBALS["shownu"]=$shownu;
if(isset($_GET['page'])){
$page=$_GET['page'];
}else $page=1;
# if $url Use the default , The null value , Assign the value to this page URL
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
#URL Analysis of the
$parse_url=parse_url($url);
@$url_query=$parse_url["query"]; // Take it out in 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";
# The page number to calculate
$lastpg=ceil($total/$shownu); // The last page , Total number of pages
$page=min($lastpg,$page);
$prepg=$page-1; // on 1 page
$nextpg=($page==$lastpg ? 0 : $page+1); // Under the 1 page
$sqlfirst=($page-1)*$shownu;
# Begin paging content
$pagecon = " According to the first ".($total?($sqlfirst+1):0)."-".min($sqlfirst+$shownu,$total)." One record, total <B>$total</B> records ";
if($lastpg<=1) return false; // If only 1 Page is out of
if($page!=1) $pagecon .=" <a href='$url=1'> Home page </a> "; else $pagecon .=" Home page ";
if($prepg) $pagecon .=" <a href='$url=$prepg'> Previous page </a> "; else $pagecon .=" Previous page ";
if($nextpg) $pagecon .=" <a href='$url=$nextpg'> After the page </a> "; else $pagecon .=" After the page ";
if($page!=$lastpg) $pagecon.=" <a href='$url=$lastpg'> back </a> "; else $pagecon .=" back ";
# Drop down the jump list , Loop through all page numbers
$pagecon .=" To the first <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, $lastpg page ";
}
}else die('pageDivide() A function of the same name already exists !');
?>