php+mysql paging query code and demonstration example
WBOY
Release: 2016-07-25 08:52:17
Original
1002 people have browsed it
//In order to avoid errors caused by repeated inclusion of files, a condition is added to determine whether the function exists:
if(!function_exists(pages)){
//Define function pages (), the meanings of the three parameters are:
//$total: the total number of information;
//$displaypg: the number of information displayed on each page, the default setting here is 20;
//$url: the link in the paging navigation, Except for adding different query information "page", the other parts are the same as this URL.
//The default value should be set to the URL of this page (i.e. $_SERVER["REQUEST_URI"]), but the right side of the default value can only be a constant, so the default value is set to an empty string, and then set to inside the function. URL of this page.
function pages($total,$displaypg=20,$url=''){
//Define several global variables:
//$page: current page number;
//$ firstcount: the starting item of (database) query;
//$pagenav: page navigation bar code, it is not output inside the function;
//$_SERVER: read the URL of this page“$_SERVER["REQUEST_URI"]&rdquo ; Necessary.
global $page,$firstcount,$pagenav,$_SERVER;
//In order to make the "$displaypg" here accessible from outside the function, set it as a global variable. Note that after a variable is redefined as a global variable, the original value is overwritten, so it is reassigned here.
$GLOBALS["displaypg"]=$displaypg;
$page=$_GET['page'];
if(!$page) $page=1;
//if $ If the url uses the default value, which is a null value, then 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"]; //Get the query string of the URL separately
if($url_query){
//Because the URL may contain page number information, It needs to be removed so that new page number information can be added.
//Pull down the jump list and loop through all page numbers:
$pagenav.= "Go to
for($i=1;$i<= $lastpg;$i++){
if($i==$page) $pagenav.="n";
else $pagenav.="< ;option value='$i'>$in";
}
$pagenav.=" page of $lastpg";
}
}
?>< /p>
Copy code
2. Mysql paging call demonstration:
$DatabaseServer="localhost";
$UserName="root";
$PassWord="";
$DatabaseName="dede";
$conn=mysql_connect($DatabaseServer,$UserName,$PassWord) or die("Database connection error"+mysql_error());
mysql_select_db($DatabaseName,$conn);
mysql_query("set names gbk ");
include("Pages.php"); //Include“pages.php”file
//Get the total number of information
$result=mysql_query("select * from dede_area ");
$total=mysql_num_rows($result);
//Call pages() to display 10 pieces of information per page (when using the default 20, this parameter can be omitted), use URL of this page (default, so omitted).
pages($total,10);
//Use global variables
$result=mysql_query("select * from dede_area limit $firstcount,$displaypg ");
while($row= mysql_fetch_array($result)){
//(list content omitted)
echo($row['eid']);
echo ($row['name']);
echo ("
");
} +mysql paging effect, as shown in the figure:
Recommended reading:
php and ajax no refresh paging code
php article paging implementation code
php limit page turning (pagination) code
PHP paging class with multiple paging methods
PHP pagination code for previous page and next page
PHP paging code for the first ten pages and the next ten pages
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn