PHP page Paging class simple and practical

Source: Internet
Author: User

<?
To avoid errors caused by repeating the inclusion of a file, add a condition that determines whether the function exists:
if (!function_exists (Pageft)) {
Define the function Pageft (), the meaning of the three parameters is:
$totle: Total information;
$DISPLAYPG: The number of messages displayed per page, which is set to default is 20;
$url: The link in the pager is the same as this one except for adding different query Information "page".
The default value should be set to the page URL (that is, $_server["Request_uri"]), but the default value can only be constant to the right, so the default value is set to an empty string, which is then set to the page URL inside the function.
function Pageft ($totle, $displaypg =20, $url = ") {
[size=-1]//defines several global variables:
$page: current page number;
$firstcount: The starting item of the (database) query;
$pagenav: the page navigation bar code, inside the function does not output it;
$_server: Read this page url "$_server[" Request_uri "]" must.
Global $page, $firstcount, $pagenav, $_server;
[size=-1]//is also set as a global variable to allow access to the "$DISPLAYPG" here, outside of the function. Note that when a variable is redefined as a global variable, the original value is overwritten, so it is re-assigned.
$GLOBALS ["Displaypg"]= $displaypg;
[Size=-1]if (! $page) $page = 1;
[size=-1]//If you use the default, or null value, the value is the URL of this page:
if (! $url) {$url =$_server["Request_uri"];}
[Size=-1]//url Analysis:
$parse _url=parse_url ($url);
$url _query= $parse _url["Query"]; Query string to take out the URL separately
if ($url _query) {
Because the URL may contain the page number information, we want to remove it, in order to add the new page number information.
Regular expressions are used here, please refer to "Regular Expressions in PHP"
$url _query=ereg_replace ("(^|&) page= $page", "", $url _query);
[size=-1]//replaces the query string for the processed URL with the query string for the original URL:
$url =str_replace ($parse _url["Query"], $url _query, $url);

Add page query information after URL, but to assign value:
if ($url _query) $url. = "&page"; else $url. = "page";
}else {
$url. = "? page";
}
[Size=-1] Page count:
$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;
[size=-1]//start page navigation bar code:
$pagenav = "Show Section <B>". ($totle? ($firstcount + 1): 0). " </B>-<B> ". Min ($firstcount + $DISPLAYPG, $totle)." </B> records, Total $totle records <BR> ";
[size=-1]//If only one page jumps out of the function:
if ($lastpg <=1) return false;
[Size=-1] $pagenav. = "<a href= ' $url =1 ' > Home </a>";
if ($PREPG) $pagenav. = "<a href= ' $url = $prepg ' > prev </a>"; else $pagenav. = "front page";
if ($NEXTPG) $pagenav. = "<a href= ' $url = $nextpg ' > Back </a>"; else $pagenav. = "Back Page";
$pagenav. = "<a href= ' $url = $lastpg ' > Last </a>";
[size=-1]//drop-down Jump List, loop to list all page numbers:
$pagenav. = "to <select name= ' topage ' size= ' 1 ' >\n";
for ($i =1; $i <= $lastpg; $i + +) {
if ($i = = $page) $pagenav. = "<option value= ' $i ' selected> $i </option>\n";
else $pagenav. = "<option value= ' $i ' > $i </option>\n";
}
$pagenav. = "</select> page, total $lastpg page";
}
}
?>

Well, the paging function is already written, Save as "pageft.php", include it when paging, and call the Pageft () function. But it doesn't output anything, but it produces several global variables for use: $firstcount, $DISPLAYPG, $pagenav.
Here is an example of how to use it:
<?
(Previous procedure slightly)
Include ("pageft.php"); Include "pageft.php" file
$page =$_get[' page '];//add this sentence
Total number of information obtained
$result =mysql_query ("SELECT * from MyTable");
$total =mysql_num_rows ($result);
if ($total ==0) die ("Temporarily no information! ");//If the record is 0
Call Pageft (), displaying 10 messages per page (this parameter can be omitted using the default 20 o'clock), using the page URL (default, so omitted).
Pageft ($total, 10);
The global variables that are generated now come in handy:
$result =mysql_query ("select * FROM MyTable limit $firstcount, $displaypg");
while ($row =mysql_fetch_array ($result)) {
(List content slightly)
}
[size=-1]//output paging navigation bar code:
Echo $pagenav;
[size=-1]//(later procedure slightly)
?>

PHP page Paging class simple and practical

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.