thinkphp pagination to write your own page class program code

Source: Internet
Author: User
Tags min prev

We can see an example of what I've done.

The code is as follows Copy Code

CREATE TABLE IF not EXISTS ' Think_form ' (
' ID ' smallint (4) unsigned not NULL auto_increment,
' title ' varchar (255) Not NULL,
' Content ' varchar (255) Not NULL,
' Create_time ' int (one) unsigned not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT Charset=utf8;


Modify the database connection information in the project configuration file, and then open the sample page to display

If you don't see the data, first manually add some data to the datasheet to test the paging effect.
The new version of the paging class can automatically configure the Url_model page link based on the current URL pattern, and you can change the differences between the page links and the Url_pathinfo_depr parameters in the configuration file. The page classes used in the
example can be downloaded separately at the extension center, and this example simply puts the pagination class under the Project Class Library directory for demonstration purposes, and in practical applications, you need to introduce a paging class library based on your actual location.
Critical Code:

The code is as follows Copy Code

Import ("@.org.page"); Import a paging class
$Form = M (' Form ');
$count = $Form->count (); Calculate Total
$Page = new Page ($count, 5);
$list = $Form->limit ($Page->firstrow. ',' . $Page->listrows)->order (' id desc ')->select ();
Simulate setting additional incoming parameters for paging
$Page->parameter = ' search=key&name=thinkphp ';
Set paging display
$Page->setconfig (' header ', ' data ');
$Page->setconfig (' A ', ' << ');
$Page->setconfig (' Last ', ' >> ');
$page = $Page->show ();

I'll combine it now.

The code is as follows Copy Code

<?php
// +----------------------------------------------------------------------
// | Refer to thinkphp pagination class change
// +----------------------------------------------------------------------
// | There are two styles: 1, the face page number is always in the middle. 2. When the page number on both sides of the page, automatically page (default).
// +----------------------------------------------------------------------
// | Author:zbseoag <zbsoeagy@qq.com>
// +----------------------------------------------------------------------
$Id: page.class.php 2013-05-26 00:08:49

Session_Start (); The session is enabled because you want to record whether the user is moving forward or paging backwards.

Class page{

Public $page; Current page


Public $nextPage; Previous page


Public $prevPage; Next page


Public $pageMax; Max Page


Public $startPage; Paging Bar at least


Public $endPage; Pagination Bar Stop Code


Public $style; Style: $style = mid


public $parameter;//number of parameters to take when page jumps


public $varPage;//Default paging variable


public $url = null; Specify URL address





protected $config = Array (


' Header ' =&gt; ' record ', ' prev ' =&gt; ' prev ', ' Next ' =&gt; ' Next ', ' first ' =&gt; ' home ', ' last ' =&gt; ' end ',


' Theme ' =&gt; '%records%%header%%page%/%pagemax% page%firstpage%%prevpage%%linkpage%%nextpage%%lastpage% '


);





/**


+----------------------------------------------------------


* Frame-making function


+----------------------------------------------------------


* Total number of records @param $records


* @param $pageSize Display the number of records per page


* @param $pageVal The parameters of the paging jump


* @param $parameter Additional parameters


+----------------------------------------------------------


*/





function __construct ($records, $pageSize, $pageVal = ' P ', $parameter = null) {





$this-&gt;records = $records;


$this-&gt;pagemax = ceil ($records/$pageSize);


$this-&gt;page= Max ($_get[$pageVal]), 1); Minimum of 1


$this-&gt;page = min ($this-&gt;page, $this-&gt;pagemax); Max for last


$this-&gt;nextpage = $this-&gt;page + 1;


$this-&gt;prevpage = $this-&gt;page-1;


$this-&gt;varpage = $pageVal; Default paging variable name


$this-&gt;parameter = $parameter; Additional URL parameters


}





/**


+----------------------------------------------------------


* Set Style


+----------------------------------------------------------


* @param $name The name of the built-in parameter


* @param $value given parameter values


+----------------------------------------------------------


*/


function Setconfig ($name, $value) {


if (Isset ($this-&gt;config[$name])) {


$this-&gt;config[$name] = $value;


}


}





/**


+----------------------------------------------------------


* Paging Display output


+----------------------------------------------------------


* @param $length Paging length


+----------------------------------------------------------


* @param $hide Whether or not to hide the previous page, such as: The current is the first page, hide the previous page


+----------------------------------------------------------


*/


Function Show ($length, $hide = False) {

        if (0 = $this->records) return ";
        Empty ($this->url)? $this->url = $_server[' Request_uri ']: null;
        $url = $this->url. (Strpos ($this->url, '? ')? ': '? '). $this->parameter;
        $parse = Parse_url ($url);
        
        if Isset ($ parse[' query ']) {
            parse_str ($parse [' Query '], $params);
            unset ($params [$this->varpage]);
            $url = $parse [' Path ']. Http_build_query ($params);
       }

if (' mid ' = = $this-&gt;style) {//Current page Center


if ($length% 2!= 0) {


$midder = Ceil ($length/2);


$big _repair = $midder-1;//When the above is rounded by one method, this is minus 1, and the other is plus 1.


}else{


$big _repair = $midder = $length/2;


}


$SML _repair = $midder-1; This is a little patch.


$this-&gt;startpage = ($this-&gt;page + $midder) &gt; $this-&gt;pagemax? $this-&gt;pagemax-$length: $this-&gt;page-$sml _repair;


$this-&gt;endpage = ($this-&gt;page &lt; $midder)? $length: $this-&gt;page + $big _repair;


}else{//Last page paging


$this-&gt;startpage = ($this-&gt;page &gt;= $length)? $_session[' startpage ': 1;


$this-&gt;endpage = $this-&gt;startpage + $length-1;





$this-&gt;lastpage = isset ($_session[' lastpage '))? $_session[' lastpage ': 1; The last page number


$_session[' lastpage ' = $this-&gt;page; Save current page





if ($this-&gt;lastpage &lt; $this-&gt;page) {//Back Page


if ($this-&gt;page &gt;= $this-&gt;endpage) {


$this-&gt;startpage = $_session[' startpage '] = $this-&gt;page;


$this-&gt;endpage = $this-&gt;startpage + $length-1;


}


}elseif ($this-&gt;lastpage = = $this-&gt;page) {//Toggle


if ($this-&gt;page = = $this-&gt;endpage) {


$this-&gt;startpage = $_session[' startpage '] = $this-&gt;page;


$this-&gt;endpage = $this-&gt;startpage + $length-1;


}else{


$this-&gt;startpage = $_session[' startpage '] = $this-&gt;page-$length + 1;


$this-&gt;endpage = $this-&gt;startpage + $length-1;


}


}else{//front Page


if ($this-&gt;page = = $this-&gt;startpage) {


$this-&gt;startpage = $_session[' startpage '] = $this-&gt;page-$length + 1;


$this-&gt;endpage = $this-&gt;startpage + $length-1;


}


}


Echo ' Start Page: '. $this-&gt;startpage. ' &lt;br/&gt; ';


Echo ' last page: '. $this-&gt;lastpage. ' &lt;br/&gt; ';


}


Fix it, please.


$this-&gt;startpage = min ($this-&gt;pagemax-$length + 1, $this-&gt;startpage); Biggest


$this-&gt;startpage = max ($this-&gt;startpage, 1);//min for first page (max first, minimum)





$this-&gt;endpage = $this-&gt;startpage + $length-1;


$this-&gt;endpage = min ($this-&gt;pagemax, $this-&gt;endpage); Max for last





$_session[' startpage ' = $this-&gt;startpage; After correction, save again





Echo ' Start Page: '. $this-&gt;startpage. ' &lt;br/&gt; ';


Echo ' last page: '. $this-&gt;lastpage. ' &lt;br/&gt; ';





if ($this-&gt;page &lt;= 1) {//If already home


$firstPage = ' &lt;span&gt; '. $this-&gt;config[' a '. ' &lt;/span&gt; ';


$prevPage = ' &lt;span&gt; '. $this-&gt;config[' prev ']. ' &lt;/span&gt; ';


}else{


$firstPage = "&lt;span&gt;&lt;a href= ' $url &amp; $this-&gt;varpage=1 ' &gt;". $this-&gt;config[' a '. ' &lt;/a&gt; ';


$prevPage = "&lt;span&gt;&lt;a href= ' $url &amp; $this-&gt;varpage= $this-&gt;prevpage ' &gt; '. $this-&gt;config[' prev '] .' &lt;/a&gt; ';


}





if ($this-&gt;page &gt;= $this-&gt;pagemax) {//If already the last page


$nextPage = ' &lt;span&gt; '. $this-&gt;config[' next '. ' &lt;/span&gt; ';


$lastPage = ' &lt;span&gt; '. $this-&gt;config[' last ']. ' &lt;/span&gt; ';


}else{


$lastPage = "&lt;a href= ' $url &amp; $this-&gt;varpage= $this-&gt;pagemax ' &gt; '. $this-&gt;config[' last ']. ' &lt;/a&gt; ';


$nextPage = "&lt;a href= ' $url &amp; $this-&gt;varpage= $this-&gt;nextpage ' &gt; '. $this-&gt;config[' next ']. ' &lt;/a&gt; ';


}





if ($hide) {//If hidden mode


($this-&gt;nextpage &gt;= $this-&gt;pagemax)? $nextPage = ': null;


($this-&gt;prevpage &lt; 1)? $prevPage = ': null;


}

for ($i = $this-&gt;startpage; $i &lt;= $this-&gt;endpage; $i + +) {


if ($this-&gt;page = = $i)


$linkPage. = "&lt;a class= ' href= ' $url &amp; $this-&gt;varpage= $i ' &gt; $i &lt;/a&gt; ';


Else


$linkPage. = "&lt;a href= ' $url &amp; $this-&gt;varpage= $i ' &gt; $i &lt;/a&gt; ';


}


$search = Array ('%records% ', '%header% ', '%page% ', '%pagemax% ', '%firstpage% ', '%prevpage% ', '%linkpage% ', '%nextpage% ') , '%lastpage% ');


$replace = Array ($this-&gt;records, $this-&gt;config[' header '), $this-&gt;page, $this-&gt;pagemax, $firstPage, $ PrevPage, $linkPage, $nextPage, $lastPage);


$PAGESTR = Str_replace ($search, $replace, $this-&gt;config[' theme '));

return $pageStr;
}

}
$page = new Page (340, 10);
$theme = '%records%%header%%page%/%pagemax% page%firstpage%%prevpage%%linkpage%%nextpage%%lastpage% ';
$page->setconfig (' theme ', $theme); You can modify the style, such as: When not the last page is, you can delete%lastpage%, or change the order of position
$page->setconfig (' Last ', ' End '); Customizable text: ' Header ' = ' record ', ' prev ' = ' prev ', ' next ' = ' next ', ' first ' = ' home ', ' last ' = ' End '
$page->url = ' http://localhost/test.php ';
echo $page->show (10);

?>
<style>
a,span{margin:2px 5px;}
. current{color:red;}
</style>

The following introduction of the official website to provide a number of measures page class effect code

The paging class is located under the Extended class library and needs to be imported before it can be used, and the following are two examples of data paging.

The first: Using the Page class and the Limit method

  code is as follows copy code

1. $User = M (' User ');//Instantiate User object
2.import (' ORG. Util.page '),//Import pagination Class
3. $count       = $User->where (' Status=1 ')->count ();// The total number of records that the query satisfies the requirement
4. $Page        = new Page ($count, 25);//Instantiate page class incoming total records and number of records displayed per page
5.$ show       = $Page->show ()//pagination display output
6.//paging data Query Note Limit method parameters to use the properties of the page class
7. $list = $User->where (' Status=1 ')->order (' Create_time ')->limit ($Page->firstrow. ', '. $Page-> listRows)->select ();
8 $this->assign (' list ', $list);//Assignment DataSet
9 $this->assign (' page ', $show);//Assignment paging output
$this-> Display (); Output template

2 The second: the implementation of pagination class and Page method

The code is as follows Copy Code
1. $User = M (' User '); Instantiating the User object
2.//Paging Data Query note the front portion of the parameter of the page method is the current number of pages used $_get[p] Get
3. $list = $User->where (' Status=1 ')->order (' Create_time ')->page ($_get[' P ']. ', a ')->select ();
4. $this->assign (' list ', $list);//Assignment Data set
5.import ("ORG.") Util.page ");//Import pagination class
6. $count = $User->where (' Status=1 ')->count ()//The total number of records that the query meets the requirements
7. $Page = new Page ($count, 25);//Materialized paging class incoming total records and number of records displayed per page
8. $show = $Page->show ()//Paging display output
9. $this->assign (' page ', $show);//Assign paging output
$this->display (); Output template

2 with query criteria

If it is a post query, how to ensure that the original query conditions can be maintained after pagination, we can pass parameters to the paging class by assigning values to the parameter property of the paging class:

The code is as follows Copy Code
1.import (' ORG. Util.page ');//Import pagination class
2. $mapcount = $User->where ($map)->count ()//The total number of records that the query meets the requirements
3. $Page = new Page ($count, 25);//Materialized paging class incoming total records and number of records displayed per page
4.//to ensure query conditions when paging jumps
5.foreach ($map as $key => $val) {
6. $Page->parameter. = "$key =". UrlEncode ($val). & ';
7.}
8. $show = $Page->show ()//Paging display output

1-page Style customization
The default paging output effect is

We can customize the paging style of the output, and page-class page provides a Setconfig method to modify the default settings. For example:
1. $page->setconfig (' header ', ' member ');

The properties supported by the Setconfig method include:
Header: Header description information, default value "bar Record"
Prev: Previous page description information, the default value is "Previous page"
Next: The following page describes the information, the default value is "Next page"
First: page description information, default is "first page"
Last: Final page description information, default value is "last Page"
Theme: Pagination topic description information, including a combination of all the above elements

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.