A useful simple PHP paging collection includes the use of method _php instances

Source: Internet
Author: User
Method One:
Copy CodeThe code is as follows:
/*
Pagination classes are used to implement pagination display on multiple data pages
version:1.0
Date:2013-10-20
*/

/*
The call is very convenient, first connect the database, directly to the SQL string of the query, you can also specify the number of data bars displayed per page
For example $pages = new Page (' SELECT * from ' zy_common_member ');
or $pages = new Page (' SELECT * from ' zy_common_member ', 10);
*/
Class page{
Private $curPage;
private $totalPages;//How many pages of data are displayed
private $dispNum;//The number of data bars displayed per page
private $queryStr;//SQL statement for query
private $limitStr;//The Limit control statement after the query statement

/*
constructor function
SQL statements that $QUERYSTR query data
$dispNum the number of data bars to display per page
*/
Public function __construct ($queryStr = ", $dispNum =10) {
$result = mysql_query ($QUERYSTR);
$totalNum = mysql_num_rows ($result);
$this->dispnum = $dispNum;
$this->totalpages = ceil ($totalNum/$dispNum);
$this->querystr = $queryStr;

$temp = (Isset ($_get["Curpage")? $_get["Curpage"]: 1);
$this->setcurpage ($temp);

$this->showcurpage ();
$this->showfoot ();
}

/* Displays the data contents of the current page */
Private Function Showcurpage () {
$this->limitstr = ' LIMIT '. (($this->curpage-1) * $this->dispnum). $this->dispnum;
Echo $this->querystr. $this->limitstr;
$result = mysql_query ($this->querystr. $this->limitstr);

if (! $result)
{
if ($this->totalpages > 0)
{
Echo ' query error '. '
';
}
Else
{
echo ' no data '. '
';
}
Return
}
$cols = Mysql_num_fields ($result);

Echo '

















'; Echo ' '; for ($i =0; $i < $cols; $i + +) {echo ' ; } Echo ' '; while ($row = Mysql_fetch_assoc ($result)) {echo ' '; foreach ($row as $key = + $value) {echo ' '; } Echo ' '; } Echo '
;
Echo Mysql_field_name ($result, $i);
Echo '
';
Echo $value;
Echo '
';
}

Private Function Setcurpage ($curPage) {
if ($curPage < 1)
{
$curPage = 1;
}
else if ($curPage > $this->totalpages)
{
$curPage = $this->totalpages;
}
$this->curpage = $curPage;
}

/*
Show pagination footer Information
such as home, Prev, Next, last and other information
*/
Private Function Showfoot () {
Echo ' Home ';
Echo ' curPage-1). ' " > previous page ';
Echo ' Curpage + 1). ' " > next page ';
Echo ' TotalPages. ' " > Last ';
}

}

?>


Method Two:
Copy CodeThe code is as follows:
Class mysqlpager{
var $pagePerNum =5;//The number of data items displayed per page
var $pagePerGroup =5;//pages per page group
var $curPage =0;//Current page, Defualt first page
var $totalPage total Pages =0;//
var $totalNum total number of =0;//data items
var $curPageGroup =0;//Current Paging group
var $curPageUrl = "";//The URL currently used for paging
var $customStyle = "";//Custom style
var $pageQuerySql = "";
function Mysqlpager () {//constructor PHP4
}
/**
* Initialization of all variables
*/
function Initallvar ($totalNum, $pagePerGroup, $curPageUrl, $curPage =1, $curPageGroup =1)
{
$this->totalnum= $totalNum;
$this->pagepergroup= $pagePerGroup;
$this->curpageurl= $curPageUrl;
$this->curpage= $curPage;
$this->curpagegroup= $curPageGroup;
}
/**
* Set the current page variable
*
* @param digital $curPage
*/
function Setcurpage ($curPage)
{
$this->curpage= $curPage;
}
/**
* Set Current Paging group variable
*
* @param mixed $curPageGroup
*/
function Setcurpagegroup ($curPageGroup)
{
$this->curpagegroup= $curPageGroup;
}
/**
* Set the URL that is currently used for the distribution class
* $CURPAGEURL String
*/
function Setcurpageurl ($CURPAGEURL)
{
$this->curpageurl= $curPageUrl;
}
/**
* Get all
*
* @param digital $totalNum
* @param digital $curPage
* @return Float
*/
function Gettotalpage ($totalNum, $curPage =0)
{
return $this->totalpage=ceil ($totalNum/$this->pagepernum);
}
/**
* Set user Custom style
*
* @param mixed $customStyle
*/
function Setcustomstyle ($customStyle)
{
$this->customstyle= $customStyle;
}
/**
* Set user custom style return string
*
*
* @param mixed $pagerString
*/
function setcustomstylestring ($pagerString)
{
return $styleString = "". $pagerString."";
}
/**
* Output Navigation page information can be used without parameters, but be sure to set the appropriate variables before use
*
* @param mixed $curPageGroup
* @param mixed $curPage
* @param mixed $CURPAGEURL
*/
function Shownavpager ($curPageGroup =0, $curPage =0, $curPageUrl =0)
{
if ($curPageGroup)
{
$this->curpagegroup= $curPageGroup;
}
if ($curPage)
{
$this->curpage= $curPage;
}
if ($CURPAGEURL)
{
$this->curpageurl= $curPageUrl;
}
$rtnString = "";
Determines whether a variable is initialized
if ($this->curpagegroup && $this->curpageurl && $this->totalnum && $this->curpage )
{
$this->totalpage= $this->gettotalpage ($this->totalnum);
if ($this->curpage==1)
$this->curpage= ($this->curpagegroup-1) * $this->pagepergroup+1;
if ($this->curpagegroup!=1)
{
$prePageGroup = $this->curpagegroup-1;
$rtnString. = "Curpageurl."? cpg= $prePageGroup > ". $this->setcustomstylestring (" << ")." ";
}
for ($i =1; $i <= $this->pagepergroup; $i + +)
{
$curPageNum = ($this->curpagegroup-1) * $this->pagepergroup+ $i;
if ($curPageNum <= $this->totalpage) {
if ($curPageNum = = $this->curpage)
{
$rtnString. = "". $this->setcustomstylestring ($curPageNum);
}else
{
$rtnString. = "curpageurl?cpg={$this->curpagegroup}&cp= $curPageNum >";
$rtnString. = $this->setcustomstylestring ($curPageNum). "";
}
}
}
if ($this->curpagegroup totalpage/$this->pagepergroup)-1)
{
$nextPageGroup = $this->curpagegroup+1;
$rtnString. = "curpageurl?cpg= $nextPageGroup >". $this->setcustomstylestring (">>"). "";
}
$this->pagequerysql= "Limit". (($this->curpage-1) * $this->pagepernum). ",". $this->pagepernum;
}
Else
{
$rtnString = "ERROR: Variable not initialized! ";
}
return $rtnString;
}
/**
* Get the full query for MySQL SQL statements
*
* @param mixed $sql
*/
function Getquerysqlstr ($sql)
{
$allsql = $sql. $this->pagequerysql;
return $allsql;
}
/**
* Set how many data items are on each page
*
* @param INT $num
*/
function Setpagepernum ($num)
{
$this->pagepernum= $num;
}
}
?>

How to use:
$curPage =$_get[' CP '];
$curPageGroup =$_get[' CPG ']
if ($curPage = = "")
$curPage = 1;
if ($curPageGroup = = "")
$curPageGroup = 1;
Starting from 1, before verifying the incoming data, anti-injection
。。。
$pager =new Mysqlpager ();
$pager->initallvar (...)
$pager->shownavpager ();
The following SQL can be any output
$sql = "SELECT id form dbname";
$querysql = $pager->getquerysqlstr ($sql)
We can get the corresponding result set by querying the database with $querysql later.


Method Three:

PHP Paging functions:
Copy CodeThe code is as follows:
<?
To avoid errors caused by duplicate include files,
Add a condition to determine whether a 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: Links in the pager, in addition to adding different query information
The part outside the page is the same as this URL.
Default value should be set to this page URL (i.e. $_server["Request_uri"])
, but only constants are set to the right of the default value, so the default value is set to an empty string
, and then set it inside the function to the page URL.
function Pageft ($totle, $displaypg =20, $url = ") {
Define 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;
To allow access to the "$DISPLAYPG" in the outside of the function,
Set it also as a global variable. Note that a variable is re-determined
The original value is overwritten when the literal is a global variable, so it is re-assigned.
$GLOBALS ["Displaypg"]= $displaypg;
if (! $page) $page = 1;
If you use the default, or null value, for the value of the page, the URL is assigned:
if (! $url) {$url =$_server["Request_uri"];}
URL Analysis:
$parse _urlparse_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,
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);

Replace 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";
}
$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;
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> ";
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";
}
}
?>

The Pageft () function is called when paging. But it doesn't output anything, but it produces several global variables for use: $firstcount, $DISPLAYPG, $pagenav.
The following examples illustrate the use of PHP paging functions:
Copy the Code code as follows:
<?
(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
(This parameter can be omitted using the default 20 o'clock),
Use this page URL (default, so omit it).
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)
}
Output paging navigation bar code:
Echo $pagenav;
(later 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.