PHP Custom Paging function

Source: Internet
Author: User
Tags define function min regular expression

PHP Custom Paging function 

Every time you write a list of pages, you almost always write a section of the page. Recently sunny little finally resolved to once and for all-customizing a paging function and saving it in the "pageft.php" file. To use the page, simply include the "pageft.php" file and call the function. Sunny little in writing this function, as much attention to the portability and ease of use of the program, the following is the function written by Sunny little:

To avoid errors caused by duplicate inclusion of files, add a condition that determines whether a function exists:

if (!function_exists (Pageft)) {

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

$totle: Total information;

$DISPLAYPG: The number of messages per page, set to default is 20;

$url: The links in pagination navigation are the same as this URL in addition to adding different query Information "page".

The default value should be set to the URL on this page (that is, $_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, which is then set to the URL of this page inside the function.

function Pageft ($totle, $displaypg =20, $url = ') {

Define several global variables:

$page: current page number;

$firstcount: (database) The starting item of the query;

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

$_server: Required to read the URL "$_server[" Request_uri "]" on this page.

Global $page, $firstcount, $pagenav, $_server;

To give the function an external access to the "$DISPLAYPG" here, it is also set as a global variable. Note that when a variable is redefined as a global variable, the original value is overwritten, so it is assigned a value here.

$GLOBALS ["Displaypg"]= $displaypg;

if (! $page) $page = 1;

If $url uses the default, or null value, the assignment is the URL to this page:

if (! $url) {$url =$_server["Request_uri"];}

URL Analysis:

$parse _url=parse_url ($url);

$url _query= $parse _url["Query"]; Query string to remove URL individually

if ($url _query) {

Because the URL may contain page number information, we need to remove it so that we can add a new page number information.

Here's a regular expression, 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);

The query string that replaces the original URL with the query string for the processed URL:

$url =str_replace ($parse _url["Query"), $url _query, $url);

Add page query information after the URL, but you want to assign a value:

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

}else {

$url. = "? page";

}

Page number calculation:

$LASTPG =ceil ($totle/$DISPLAYPG); Last page, 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;

To start the pager bar code:

$pagenav = "Show First". ($totle? ($firstcount + 1): 0). " -". Min ($firstcount + $DISPLAYPG, $totle)." Records, Total $totle records

";

If only one page jumps out of the function:

if ($lastpg <=1) return false;

$pagenav. = "Home page";

if ($PREPG) $pagenav. = "front page"; else $pagenav. = "front page";

if ($NEXTPG) $pagenav. = "Back Page"; else $pagenav. = "Back Page";

$pagenav. = "Last";

Drop-down Jump List, looping through all page numbers:

$pagenav. = "to Nth";   For ($i =1 $i <= $lastpg $i + +) {if ($i = = $page) $pagenav. = "$in";   else $pagenav. = "$in"; $pagenav. = "page, total $lastpg page";

}

}

?>

 Well, the paging function has been written, saved as "pageft.php", contains it when paging, and invokes the Pageft () function. However, it does not output anything, but produces several global variables for use: $firstcount, $DISPLAYPG, $pagenav. The following examples illustrate its usage: 

(Previous procedure slightly)

Include ("pageft.php"); Include "pageft.php" file

Total number of information obtained

$result =mysql_query ("SELECT * from MyTable");

$total =mysql_num_rows ($result);

Call Pageft (), display 10 messages per page (using the default 20 o'clock, you can omit this parameter), use this page URL (default, so omitted).

Pageft ($total, 10);

The global variables that are produced now come in handy:

$result =mysql_query ("select * FROM MyTable limit $firstcount, $displaypg");

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

(List content slightly)

}

Output pagination navigation bar code:

Echo $pagenav;

(Follow the procedure slightly)

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.