Home > Article > Backend Development > PHP paging function sample code sharing_PHP tutorial
Share an example of PHP paging function code. It is very good to use this function to implement paging code.
Code, php paging function.
//Database operation
$result=mysql_query("select * from mytable limit $sqlfirst,$shownu", $myconn);
while($row=mysql_fetch_array($result)){
...Your operation
}
echo $pagecon; //Output paging navigation content
*/
if(!function_exists("pageDivide")){
#$total Total number of information
#$shownu Display quantity, default 20
#$url Link to this page
function pageDivide($total ,$shownu=20,$url=''){
#$page current page number
#$sqlfirst mysql database starting item
#$pagecon Paging navigation content
global $page,$sqlfirst,$pagecon,$_SERVER;
$GLOBALS[ "shownu"]=$shownu;
if(isset($_GET['page'])){
$page=$_GET['page'];
}else $page=1;
#If $url uses the default value, which is an empty value, the value is assigned to the URL of this page
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
#URL analysis
$parse_url=parse_url($url);
@$url_query=$parse_url["query"]; //Extract the content after the question mark?
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";
#Page number calculation
$lastpg=ceil($total/$shownu); //Last page, total number of pages
$page=min($lastpg,$page);
$prepg= $page-1; //Previous page
$nextpg=($page==$lastpg ? 0 : $page+1); //Next page
$sqlfirst=($page-1)* $shownu;
#Start paging navigation content
$pagecon = "Show the ".($total?($sqlfirst+1):0)."-".min($sqlfirst+$shownu,$total)." Records, a total of $total records";
if($lastpg<=1) return false; //If there is only one page, jump out
if($page!=1) $pagecon .=" Homepage "; else $pagecon .="Homepage";
if ($prepg) $pagecon .=" Previous page "; else $pagecon .=" Previous page";
if($nextpg) $pagecon .=" Last page "; else $pagecon .=" Next page";
if($page!=$lastpg) $pagecon.=" Last page "; else $pagecon .=" Last page";
# Drop down jump list, loop through all page numbers
$pagecon .=" Go to page of $lastpg";
}
}else die('pageDivide() function with the same name already exists!');
?>