PHP custom paging function_PHP tutorial

WBOY
Release: 2016-07-13 10:22:53
Original
948 people have browsed it

PHP custom paging function

 PHP custom paging function

Every time you write a list page, you almost have to write a paging program. Recently, Xiaoyang finally decided to do it once and for all - to customize a paging function and save it in the "pageft.php" file. If you want to use paging, just include the "pageft.php" file directly and call this function. When Xiaoyang wrote this function, he tried his best to pay attention to the portability and ease of use of the program. The following is the function written by Xiaoyang:

//In order to avoid errors caused by repeated inclusion of files, a condition for determining whether the function exists:

 if(!function_exists(pageft)){

//Define the function pageft(), the meaning of the three parameters is:

//$totle: total number of messages;

//$displaypg: The number of information displayed on each page, the default setting here is 20;

//$url: The link in the paging navigation is the same as this URL except for adding different query information "page".

// 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 inside the function Set as the URL of this page.

Function pageft($totle,$displaypg=20,$url=''){

//Define several global variables:

//$page: current page number;

//$firstcount: (database) starting item of query;

//$pagenav: page navigation bar code, it is not output inside the function;

//$_SERVER: necessary to read the URL "$_SERVER["REQUEST_URI"]" of this page.

global $page,$firstcount,$pagenav,$_SERVER;

//To make "$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;

 if(!$page) $page=1;

//If $url uses the default value, which is empty, then the assigned value is 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, we need to remove it in order to add new page number information.

//Regular expressions are used here, please refer to "Regular Expressions in PHP" (http://www.pconline.com.cn/pcedu/empolder/wz/php/10111/15058.html)

$url_query=ereg_replace("(^|&)page=$page","",$url_query);

//Replace the query string of the processed URL with the query string of the original URL:

$url=str_replace($parse_url["query"],$url_query,$url);

//Add page query information after the URL, but wait for assignment:

 if($url_query) $url.="&page"; else $url.="page";

 }else {

$url.="?page";

 }

Page number calculation:

 $lastpg=ceil($totle/$displaypg); //The last page is also the total number of pages

$page=min($lastpg,$page);

 $prepg=$page-1; //Previous page

 $nextpg=($page==$lastpg ? 0 : $page+1); //Next page

 $firstcount=($page-1)*$displaypg;

//Start paging navigation bar code:

 $pagenav="Display the ".($totle?($firstcount+1):0)."-".min($firstcount+$displaypg,$totle)." records, a total of $totle records

 ";

//If there is only one page, jump out of the function:

 if($lastpg<=1) return false;

 $pagenav.=" Home ";

 if($prepg) $pagenav.=" previous page "; else $pagenav.=" previous page ";

 if($nextpg) $pagenav.=" next page "; else $pagenav.=" next page ";

 $pagenav.=" Last page ";

// Pull down the jump list and list all page numbers in a loop:

$pagenav.="To n"; for($i=1;$i<=$lastpg;$i++){ if($i==$page) $pagenav.="$in"; else $pagenav.="$in"; } $pagenav.=" Page, total $lastpg page";

 }

 }

 ?>

 Okay, the paging function has been written and saved as "pageft.php". When you want to paginate, include it and call the pageft() function. However, it does not output anything, but generates several global variables for use: $firstcount, $displaypg, $pagenav. The following examples illustrate its usage: 

// (The previous procedure is omitted)

include("pageft.php"); //Include the "pageft.php" file

//Get the total number of information

$result=mysql_query("select * from mytable");

 $total=mysql_num_rows($result);

//Call pageft() to display 10 pieces of information per page (this parameter can be omitted when using the default 20), and use the URL of this page (default, so omit it).

pageft($total,10);

//The global variables generated now come in handy:

$result=mysql_query("select * from mytable limit $firstcount,$displaypg ");

while($row=mysql_fetch_array($result)){

//(List content omitted)

 }

//Output paging navigation bar code:

echo $pagenav;

// (The following procedures are omitted)

 ?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/845138.htmlTechArticlePHP custom paging function PHP custom paging function  Every time you write a list page, you almost have to write A paging program. Recently, Xiaoyang finally decided to customize one once and for all...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!