PHP page-Class Collection _php example

Source: Internet
Author: User
Tags define function

Paging Class One

Copy Code code as follows:

<?php
/**
Paging class
Modification: Silence
Creatdate:2006-5-30
Lastmodify:2009-5-31
How to use
$page = new Page ($result, 20); $result is an array of returned recordsets, 20 is the number of bars returned per page
$index = $page->getindexbar (). $page->getpageinfo ();
Print_r ($result);
echo \ "<br><br>\";
echo \ "<center>\" $index. \ "</center>\";
*/
Class Page {
Private $mTotalRowsNum = 0; Total number of lines of information
Private $mCurPageNumber = 1; Current page
Private $mTotalPagesNum = 1; Total pages
Private $mQueryString; The data passed by the page (the string after the URL?)
Private $mPageRowsNum = 20; Show number of rows per page
Private $mIndexBarLength = 11; Number of pages in the index bar
Private $mIndexBar = '; Page Number Index Bar
Private $mPageInfo = '; Paging information
Page number index bar style
Private $mNextButton = \ "<font style=\\" font-family:webdings\\ ">8</font>\";
Private $mPreButton = \ "<font style=\\" font-family:webdings\\ ">7</font>\";
Private $mFirstButton = \ "<font style=\\" font-family:webdings\\ ">9</font>\";
Private $mLastButton = \ "<font style=\\" font-family:webdings\\ ">:</font>\";
Private $mCssIndexBarCurPage = \ "Font-weight:bold;color: #FF0000 \";
Private $mCssIndexBarPage = ';
Pagination Information Style
Private $mCssPageInfoNumFont = ' color: #FF0000 ';
Private $mCssPageInfoFont = ';
Construction method
Public function __construct (& $rSqlQuery, $userPageRowsNum = ') {
if (! Is_array ($rSqlQuery)) {
$this->setdbpagebreak ($rSqlQuery, $userPageRowsNum);
} else {
$this->setarraypagebreak ($rSqlQuery, $userPageRowsNum);
}
}
Set up a database-type paging
Private Function Setdbpagebreak (& $rSqlQuery, $userPageRowsNum = ') {
$this->setdbtotalrowsnum ($rSqlQuery);
$this->settotalpagesnum ($userPageRowsNum);
if ($this->mtotalpagesnum > 1) {
$this->setcurpagenumber ();
$this->setsqlquery ($rSqlQuery);
$this->setquerystring ();
$this->setindexbar ();
$this->setpageinfo ();
}
}
Set up an array of pagination
Private Function Setarraypagebreak (& $rArray, $userPageRowsNum = ', $userTotalRowsNum = ') {
$this->setarraytotalrowsnum ($rArray, $userTotalRowsNum);
$this->settotalpagesnum ($userPageRowsNum);
if ($this->mtotalpagesnum > 1) {
$this->setcurpagenumber ();
$this->setarray ($rArray);
$this->setquerystring ();
$this->setindexbar ();
$this->setpageinfo ();
}
}
The database type calculates the total number of rows
Private Function Setdbtotalrowsnum ($rSqlQuery) {
$this->mtotalrowsnum = mysql_num_rows (mysql_query ($rSqlQuery));
}
Array type to calculate total number of rows
Private Function Setarraytotalrowsnum ($array) {
$this->mtotalrowsnum = count ($array);
}
Calculate Total Pages
Private Function settotalpagesnum ($userPageRowsNum = ' ") {
if ($userPageRowsNum) {
$this->mpagerowsnum = $userPageRowsNum;
}
$this->mtotalpagesnum = (int) (Floor ($this->mtotalrowsnum-1)/$this->mpagerowsnum) + 1);
}
Calculate Current Pages
Private Function Setcurpagenumber () {
if ($_get [' page ']) {
$this->mcurpagenumber = $_get [' page '];
}
}
Fix SQL Intercept statement
Private Function Setsqlquery (& $rSqlQuery) {
$start _number = ($this->mcurpagenumber-1) * $this->mpagerowsnum;
$rSqlQuery. = \ "LIMIT \". $start _number. \",\" . $this->mpagerowsnum;
}
Fixed the intercepted array
Private Function SetArray (& $rArray) {
$start _number = ($this->mcurpagenumber-1) * $this->mpagerowsnum;
$rArray = Array_slice ($rArray, $start _number, $this->mpagerowsnum);
}
Fixed $_get pass Data
Private Function setquerystring () {
$query _string = $_server [' query_string '];
if ($query _string = = ") {
$this->mquerystring = "page=\";
} else {
$this->mquerystring = preg_replace ("/&?page=\d+/\", "", $query _string);
$this->mquerystring = \ "? \". $this->mquerystring. \ "&page=\";
}
}
Set page number Index bar
Private Function Getpageindex () {
if ($this->mtotalpagesnum <= $this->mindexbarlength) {
$first _number = 1;
$last _number = $this->mtotalpagesnum;
} else {
$offset = (int) floor ($this-&GT;MINDEXBARLENGTH/2);
if (($this->mcurpagenumber-$offset) <= 1) {
$first _number = 1;
} elseif (($this->mcurpagenumber + $offset) > $this->mtotalpagesnum) {
$first _number = $this->mtotalpagesnum-$this->mindexbarlength + 1;
} else {
$first _number = $this->mcurpagenumber-$offset;
}
$last _number = $first _number + $this->mindexbarlength-1;
}
$last _number;
for ($i = $first _number; $i <= $last _number; $i + +) {
if ($this->mcurpagenumber = = $i) {
$page _index. = \ "<font style= ' \". $this->mcssindexbarcurpage. \ "' >\". $i. \ "</font>";
} else {
$page _index. = \ "<a href= ' \". $this->mquerystring. $i. \ "' Style= '". $this->mcssindexbarpage. \ "' >\". $i. \ "</a>";
}
}
return $page _index;
}
Set page number Index bar
Private Function Setindexbar () {
$this->mindexbar = $this->getnavfirstbutton ();
$this->mindexbar. = $this->getnavprebutton ();
$this->mindexbar. = $this->getpageindex ();
$this->mindexbar. = $this->getnavnextbutton ();
$this->mindexbar. = $this->getnavlastbutton ();
}
Get page number Index bar Home button
Private Function Getnavfirstbutton () {
return \ "<a href= '". $this->mquerystring. \ "1 ' >\". $this->mfirstbutton. \ "</a>";
}
Get page number index bar prev button
Private Function Getnavprebutton () {
if ($this->mcurpagenumber > 1) {
$pre _number = $this->mcurpagenumber-1;
} else {
$pre _number = 1;
}
return \ "<a href= '". $this->mquerystring. $pre _number. \ "' >\". $this->mprebutton. \ "</a>";
}
Get page number index bar next page button
Private Function Getnavnextbutton () {
if ($this->mcurpagenumber < $this->mtotalpagesnum) {
$next _number = $this->mcurpagenumber + 1;
} else {
$next _number = $this->mtotalpagesnum;
}
return \ "<a href= '". $this->mquerystring. $next _number. \ "' >\". $this->mnextbutton. \ "</a>";
}
Get page number index bar last button
Private Function Getnavlastbutton () {
return \ "<a href= '". $this->mquerystring. $this->mtotalpagesnum. \ "' >\". $this->mlastbutton. \ "</a>";
}
Set up paging information
Private Function Setpageinfo () {
$this->mpageinfo = \ "<font style= '". $this->mcsspageinfofont. \ "' >\";
$this->mpageinfo. = \ "Altogether <font style= ' \". $this->mcsspageinfonumfont. \ "' >\". $this->mtotalrowsnum. \ "</font> Message | \";
$this->mpageinfo. = \ "<font style= ' \". $this->mcsspageinfonumfont. \ "' >\". $this->mpagerowsnum. \ </font> Article/page | \";
$this->mpageinfo. = \ "Altogether <font style= ' \". $this->mcsspageinfonumfont. \ "' >\". $this->mtotalpagesnum. \ </font> Page | \";
$this->mpageinfo. = \ "<font style= ' \". $this->mcsspageinfonumfont. \ "' >\". $this->mcurpagenumber. \ "</font> page \";
$this->mpageinfo. = \ "</font>\";
}
Take out the page number index bar
Public Function Getindexbar () {
return $this->mindexbar;
}
To remove the paging information
Public Function GetPageInfo () {
return $this->mpageinfo;
}
Release class
function __destruct () {
}
}
?>

Page-Class Two

Copy Code code as follows:

<?php
/*
* Created on 2007-6-8
* Programmer:alan, Msn-haowubai@hotmail.com
* Kebeke.com Develop a project Php-mysql-apache
* Window-preferences-phpeclipse-php-code Templates
*/
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"
$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 <B>". ($totle? ($firstcount + 1): 0). " </B>-<B> ". Min ($firstcount + $DISPLAYPG, $totle)." </B> Records, a total of $totle records ";
If only one page jumps out of the function:
if ($lastpg <=1) return false;
$pagenav. = "<a href= ' $url =1 ' > Home </a>";
if ($PREPG) $pagenav. = "<a href= ' $url = $prepg ' > front page </a>"; else $pagenav. = "front page";
if ($NEXTPG) $pagenav. = "<a href= ' $url = $nextpg ' > Back page </a>"; else $pagenav. = "Back Page";
$pagenav. = "<a href= ' $url = $lastpg ' > Last </a>";
Drop-down Jump List, looping through all page numbers:
$pagenav. = "to <select name= ' topage ' size= ' 1 ' onchange= ' window.location=\" $url =\ "+this.value ' >\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";
}
}

Examples of-------------------------specific demonstrations-------------------------------
/*
(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)
*/
?>

Three of the page-type series

Copy Code code as follows:

<?php
Class Page {
Private $total; Query the total number of all data records
Private $page; Current first few pages
Private $num; Number of bars per page showing records
Private $pageNum; How many pages are there altogether?
Private $offset; The start offset of the record from the database
function __construct ($total, $page =1, $num =5) {
$this->total= $total;
$this->page= $page;
$this->num= $num;
$this->pagenum= $this->getpagenum ();
$this->offset= $this->getoffset ();
}
Private Function Getpagenum () {
Return Ceil ($this->total/$this->num);
}
Private Function Getnextpage () {
if ($this->page== $this->pagenum)
return false;
Else
return $this->page+1;
}
Private Function Getprevpage () {
if ($this->page==1)
return false;
Else
return $this->page-1;
}
Offset from database query
Private Function GetOffset () {
Return ($this->page-1) * $this->num;
}
The number of records at the beginning of the current page
Private Function Getstartnum () {
if ($this->total==0)
return 0;
Else
return $this->offset+1;
}
Number of records at the end of the current page
Private Function Getendnum () {
return min ($this->offset+ $this->num, $this->total);
}
Public Function GetPageInfo () {
$pageInfo =array (
"Row_total" => $this->total,
"Row_num" => $this->num,
"Page_num" => $this->getpagenum (),
"Current_page" => $this->page,
"Row_offset" => $this->getoffset (),
"Next_page" => $this->getnextpage (),
"Prev_page" => $this->getprevpage (),
"Page_start" => $this->getstartnum (),
"Page_end" => $this->getendnum ()
);
return $pageInfo;
}
}
?>

3 Kinds of paging classes are very efficient to use, but also very simple, here recommended to everyone.

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.