ThinkPHP paging class usage problems, thinkphp paging. ThinkPHP paging class usage problems, thinkphp paging ThinkPHP3.2.3Page. class. php file source code? Php + ----------------------------------------------------------------------- ThinkPHP paging class usage problems, thinkphp paging
ThinkPHP3.2.3Page. class. php file source code
'% TOTAL_ROW % record', 'prev' => '<', 'next' => ', 'first' => '1... ', 'last' => '... % TOTAL_PAGE % ', 'theme' => '% FIRST % UP_PAGE % LINK_PAGE % DOWN_PAGE % END % ',); /*** architecture function ** @ param array $ total number of records of totalRows * @ param array $ number of records displayed on each page of listRows * @ param array $ parameter for paging jump */public function _ construct ($ totalRows, $ listRows = 20, $ parameter = array () {C ('Var _ page') & $ this-> p = C ('Var _ page ');/ /Set the paging parameter name/* Basic Settings */$ this-> totalRows = $ totalRows; // set the total number of records $ this-> listRows = $ listRows; // set the number of lines displayed on each page. $ this-> parameter = empty ($ parameter )? $ _ GET: $ parameter; $ this-> nowPage = empty ($ _ GET [$ this-> p])? 1: intval ($ _ GET [$ this-> p]); $ this-> nowPage = $ this-> nowPage> 0? $ This-> nowPage: 1; $ this-> firstRow = $ this-> listRows * ($ this-> nowPage-1 );} /*** custom paging link setting * @ param string $ name set name * @ param string $ value set value */public function setConfig ($ name, $ value) {if (isset ($ this-> config [$ name]) {$ this-> config [$ name] = $ value ;}} /*** generate link URL * @ param integer $ page number * @ return string */private function url ($ PAGE) {return str_replace (urlencode ('[page]'), $ page, $ This-> url);}/*** assemble the paging link * @ return string */public function show () {if (0 = $ this-> totalRows) return '';/* generate URL */$ this-> parameter [$ this-> p] = '[ PAGE] '; $ this-> url = U (ACTION_NAME, $ this-> parameter);/* calculate the page information */$ this-> totalPages = ceil ($ this-> totalRows/$ this-> listRows ); // total number of pages if (! Empty ($ this-> totalPages) & $ this-> nowPage> $ this-> totalPages) {$ this-> nowPage = $ this-> totalPages ;} /* calculate the temporary paging variable */$ now_cool_page = $ this-> rollPage/2; $ now_cool_page_ceil = ceil ($ now_cool_page ); $ this-> lastSuffix & $ this-> config ['last'] = $ this-> totalPages; // Previous Page $ up_row = $ this-> nowPage-1; $ up_page = $ up_row> 0? '$ This-> url ($ up_row ). '"> '. $ this-> config ['prev']. '':''; // Next Page $ down_row = $ this-> nowPage + 1; $ down_page = ($ down_row <= $ this-> totalPages )? '$ This-> url ($ down_row ). '"> '. $ this-> config ['Next']. '':''; // The first page $ the_first = ''; if ($ this-> totalPages> $ this-> rollPage & ($ this-> nowPage-$ now_cool_page)> = 1) {$ the_first = '$ this-> url (1 ). '"> '. $ this-> config ['first']. '';} // The Last Page $ the_end =''; if ($ this-> totalPages> $ this-> rollPage & ($ this-> nowPage + $ now_cool_page) <$ this-> totalPages) {$ the_end = '$ this-> url ($ this-> totalPages). '"> '. $ This-> config ['last']. '';} // Number connection $ link_page =" "; for ($ I = 1; $ I <= $ this-> rollPage; $ I ++) {if ($ this-> nowPage-$ now_cool_page) <= 0) {$ page = $ I;} elseif ($ this-> nowPage + $ now_cool_page-1)> = $ this-> totalPages) {$ page = $ this-> totalPages-$ this-> rollPage + $ I ;} else {$ page = $ this-> nowPage-$ now_cool_page_ceil + $ I;} if ($ page> 0 & $ page! = $ This-> nowPage) {if ($ page <= $ this-> totalPages) {$ link_page. = '$ this-> url ($ page ). '"> '. $ page. '';} else {break;} else {if ($ page> 0 & $ this-> totalPages! = 1) {$ link_page. = ''. $ page. '';}}// replace the page content $ page_str = str_replace (array ('% HEADER %', '% NOW_PAGE %', '% UP_PAGE % ', '% DOWN_PAGE %', '% FIRST %', '% LINK_PAGE %', '% END %', '% TOTAL_ROW %', '% TOTAL_PAGE % '), array ($ this-> config ['head'], $ this-> nowPage, $ up_page, $ down_page, $ the_first, $ link_page, $ the_end, $ this-> totalRows, $ this-> totalPages), $ this-> config ['theme ']); return"
{$ Page_str}
";}}
After data query, the dataset is usually paged. ThinkPHP also provides a paging class to support data paging. The following are two examples of data paging.
First: use the Page class and limit method
$ User = M ('user'); // instantiate the User object $ count = $ User-> where ('status = 1')-> count (); // query the total number of records meeting the requirements $ Page = new \ Think \ Page ($ count, 25 ); // The total number of incoming records and the number of records displayed on each Page (25) $ show = $ Page-> show (); // Display output by Page // query paging data. Note that the parameters of the limit method must use the attribute $ list = $ User-> where ('status = 1') of the Page class ') -> order ('create _ time')-> limit ($ Page-> firstRow. ','. $ Page-> listRows)-> select (); $ this-> assign ('list', $ list); // assign a dataset $ this-> assign ('page ', $ show); // value-assigned paging output $ this-> display (); // output Template
Second: Implementation of paging and page methods
$ User = M ('user '); // instantiate the User object // perform paging Data Query. Note that the first part of the page method parameter is the current page number. use $ _ GET [p] to GET $ list = $ User-> where ('status = 1 ') -> order ('create _ time')-> page ($ _ GET ['P']. ', 25')-> select (); $ this-> assign ('list', $ list ); // assign a dataset $ count = $ User-> where ('status = 1')-> count (); // query the total number of records meeting the requirements $ Page = new \ Think \ Page ($ count, 25 ); // instantiate the total number of incoming records in the paging class and the number of records displayed on each Page $ show = $ Page-> show (); // display output by page $ this-> assign ('page', $ show); // value-assigned paging output $ this-> display (); // output Template
Bring query conditions
For POST-based queries, how can we ensure that the original query conditions can be maintained after paging? we can input parameters to the paging class by assigning values to the parameter attribute of the paging class.
$ Count = $ User-> where ($ map)-> count (); // query the total number of records meeting the requirements $ Page = new \ Think \ Page ($ count, 25); // instantiate the total number of records passed in by the paging class and the number of records displayed on each page // ensure the query condition foreach ($ map as $ key => $ val) during paging jump) {$ Page-> parameter [$ key] = urlencode ($ val) ;}$ show = $ Page-> show (); // Display output by Page
Paging style customization
We can customize the output pagination style. pagination pages provide a setConfig method to modify some default settings. For example:
$ Page-> setConfig ('header ','Total% TOTAL_ROW %Record No.% NOW_PAGE %Page/total% TOTAL_PAGE %Page'); $ Page-> setConfig ('prev', 'Previous page'); $ page-> setConfig ('next', 'next page '); $ page-> setConfig ('first ', 'homepage'); $ page-> setConfig ('last', 'tail '); $ page-> setConfig ('theme ',' % FIRST % UP_PAGE % LINK_PAGE % DOWN_PAGE % END % HEADER % ');
The setConfig method supports the following attributes:
Header: header description. the default value is "% TOTAL_ROW % Records"
Next: next Page Description. default value: ">"
First: the description on the first page. the default value is "1 ..."
Last: the description on the last page. the default value is "…". % TOTAL_PAGE %"
Theme: The description of the pagination topic, including the combination of all the above elements. setting this attribute can change the display position of each unit on the page, the default value is "% FIRST % UP_PAGE % LINK_PAGE % DOWN_PAGE % END %"
The relationship between the display locations is as follows:
Location description
% FIRST % indicates that the link on the FIRST page is displayed.
% UP_PAGE % indicates that the link on the previous page is displayed.
% LINK_PAGE % indicates pagination link display
% DOWN_PAGE % indicates that the link on the next page is displayed.
% END % indicates that the link on the last page is displayed.
In addition to changing the display information, you can also use styles to define the display effect of pages. These Style classes include: first (first page), prev (previous page), next (next page), end (last page), num (numbers on other pages), current (current page ).
ThinkPHP system paging writing is well-developed. it takes only a few minutes to study the source code. official comments are also clear. The following describes the problems in use. By default
Public $ rollPage = 11; // The number of pages displayed on each page in the paging column public $ lastSuffix = true; // whether the total number of pages is displayed on the last page
The comment is not very clear at the beginning. In fact, the first parameter is to control the number of pages displayed. if 11 pages are displayed by default, it is usually too long. I like to set it to 5, of course, you should not modify these parameters in the system source file, but set them when using them.$page->rollPage=5You can.
The second parameter is quite tricky. we usually useSetConfig ('last', 'tail ');It is used to set the last page to "last page", but in the show method, you can see that$this->lastSuffixReset$this->config['last']The value is the total number of pages, so that no matter how you set the last business to be displayed as a number. I don't know why the official website is set this way. The solution is to reset it.$page->lastSuffix = false;You can.
When ThinkPHP3.2.3 Page. class. php file source code? Php // + ---------------------------------------------------------------------...