A practical simple PHP paging collection includes using method _php instances

Source: Internet
Author: User
Tags define function mixed prev

Method One:

Copy Code code as follows:



<?php


/*


Pagination classes are used to display multiple data pagination


version:1.0


Date:2013-10-20


*/





/*


The call is very convenient, first connect the database, directly descendants of the query SQL string, you can also specify the number of data 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;//Data total number of pages displayed


Private $dispNum//number of data bars displayed per page


Private $QUERYSTR The SQL statement of the query


private $limitStr;//Limit Control statement after query statement





/*


Constructors


$QUERYSTR SQL statement to query data


$dispNum the number of data bars displayed 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 ();


}





/* Display the data content 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 '. ' <br> ';


}


Else


{


echo ' no data '. ' <br> ';


}


Return


}


$cols = Mysql_num_fields ($result);





Echo ' <table border= 1 ' > ';


Echo ' <tr> ';


for ($i =0; $i < $cols; $i + +)


{


Echo ' <th> ';


Echo Mysql_field_name ($result, $i);


Echo ' </th> ';


}


Echo ' </tr> ';





while ($row = Mysql_fetch_assoc ($result))


{


Echo ' <tr> ';


foreach ($row as $key => $value)


{


Echo ' <td> ';


Echo $value;


Echo ' </td> ';


}


Echo ' </tr> ';


}





Echo ' </table> ';


}





Private Function Setcurpage ($curPage) {


if ($curPage < 1)


{


$curPage = 1;


}


else if ($curPage > $this->totalpages)


{


$curPage = $this->totalpages;


}


$this->curpage = $curPage;


}





/*


Display the page footer information


such as home, prev, next page, last etc information


*/


Private Function Showfoot () {


Echo ' <a href= "? curpage=1" > Home </a> ';


Echo ' <a href= '? curpage= '. ($this->curpage-1). " > Prev </a> ';


Echo ' <a href= '? curpage= '. ($this->curpage + 1). " > next page </a> ';


Echo ' <a href=, curpage= '. $this->totalpages. ' " > Last </a> ';


}





}





?>



Method Two:

Copy Code code as follows:



<?php


Class mysqlpager{


var $pagePerNum =5;//The number of data items per page


var $pagePerGroup =5;//pages in each paging group


var $curPage =0;//Current page, Defualt first page


=0;//var $totalPage total pages


Total number of =0;//data items in Var $totalNum


var $curPageGroup =0;//Current Paging group


var $curPageUrl = "";//The URL that is currently used for the 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 Current page variable


*


* @param digital $curPage


*/


function Setcurpage ($curPage)


{


$this->curpage= $curPage;


}


/**


* Set the current page-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 = "<span class=". $customStyle. " > ". $pagerString." </span> ";


}


/**


* Output Navigation page information can be used without parameters, but you must set the corresponding variable 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. = "<a href=". $this->curpageurl. "? cpg= $prePageGroup > ". $this->setcustomstylestring (" << ")." </a> ";


}


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. = "<a href= $this->curpageurl?cpg={$this->curpagegroup}&cp= $curPageNum >";


$rtnString. = $this->setcustomstylestring ($curPageNum). </a> ";


}


}


}


if ($this->curpagegroup<ceil ($this->totalpage/$this->pagepergroup)-1)


{


$nextPageGroup = $this->curpagegroup+1;


$rtnString. = "<a href= $this->curpageurl?cpg= $nextPageGroup >". $this->setcustomstylestring (">>") ." </a> ";


}


$this->pagequerysql= "Limit". (($this->curpage-1) * $this->pagepernum). ",". $this->pagepernum;


}


Else


{


$rtnString = "ERROR: Variable not initialized!" ";


}


return $rtnString;


}


/**


* Get complete query for MySQL SQL statements


*


* @param mixed $sql


*/


function Getquerysqlstr ($sql)


{


$allsql = $sql. $this->pagequerysql;


return $allsql;


}


/**


* Set how many data items per 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;


Are all starting from 1, before validating the incoming data, preventing injection


。。。


$pager =new Mysqlpager ();


$pager->initallvar (...)


$pager->shownavpager ();


The following SQL can be any output


$sql = "SELECT id form dbname";


$querysql = $pager->getquerysqlstr ($sql)


Later, we can query the database with $querysql to get the corresponding result set.


Method Three:

PHP Paging function:

Copy Code code as follows:



<?


In order to avoid duplicate inclusion of files that cause errors,


Added 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: Links in pagination navigation, in addition to adding different query information


The parts outside of "page" are the same as this URL.


The default value should be set to this page URL (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


, and then set the URL on 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 allow the function to access the "$DISPLAYPG" here,


Set it as a global variable as well. Note that a variable is reset


When the value is a global variable, the original is overwritten, so it is assigned a new 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 _urlparse_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,


To add the 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";


}


$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 < 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";


}


}


?>


Call the Pageft () function when paging. However, it does not output anything, but produces several global variables for use: $firstcount, $DISPLAYPG, $pagenav.
The following examples illustrate the use of the PHP paging function:

Copy 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 by using the default 20 o'clock),
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.