php Paging Function Complete Instance Code

  • 2021-07-18 07:40:24
  • OfStack

This article shares a complete example of php paging function code, using this function to achieve paging effect is very good. Share it for your reference.

Specific function codes are as follows:


<?php
/*
* Created on 2011-07-28
*  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 actions 
}
echo $pagecon;  // Output paging navigation content 
*/

if(!function_exists("pageDivide")){
#$total    Total information 
#$shownu   Display quantity , Default 20
#$url    Links to this page 
function pageDivide($total,$shownu=20,$url=''){

#$page  Current page number 
#$sqlfirst mysql Database starter 
#$pagecon   Paging navigation content 
global $page,$sqlfirst,$pagecon,$_SERVER;
$GLOBALS["shownu"]=$shownu;

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

# If $url Use default , Null value , Is assigned to this page URL
if(!$url){ $url=$_SERVER["REQUEST_URI"];}

#URL Analysis 
$parse_url=parse_url($url);
@$url_query=$parse_url["query"];  // Take it out in the question mark ? Subsequent 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";

# Page number calculation 
$lastpg=ceil($total/$shownu);  // Last page , Total pages 
$page=min($lastpg,$page);
$prepg=$page-1; // Upper 1 Page 
$nextpg=($page==$lastpg ? 0 : $page+1); // Under 1 Page 
$sqlfirst=($page-1)*$shownu;

# Start paging content 
$pagecon = " Display number  ".($total?($sqlfirst+1):0)."-".min($sqlfirst+$shownu,$total)."  Records, total  <B>$total</B>  A record ";
if($lastpg<=1) return false;  // If only 1 The page jumps out 

if($page!=1) $pagecon .=" <a href='$url=1'> Home page </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'> End page </a> "; else $pagecon .="  End page  ";

# Drop-down jump list , Loop 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>  Pages, in total  $lastpg  Page ";

}
}else die('pageDivide() A function with the same name already exists !');
?>

I believe that this paper has a certain reference value for everyone's study of PHP programming.


Related articles: