


Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial
Jul 12, 2016 am 08:59 AMIssues related to the use of ThinkPHP paging class, thinkphp paging
ThinkPHP3.2.3<span> Page</span>.<span>class</span>.<span>php文件源碼 </span><?<span>php </span><span>//</span><span> +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] // +---------------------------------------------------------------------- // | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved. // +---------------------------------------------------------------------- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) // +---------------------------------------------------------------------- // | Author: 麥當(dāng)苗兒 <zuojiazi@vip.qq.com> <http://www.zjzit.cn> // +----------------------------------------------------------------------</span> <span>namespace Think; </span><span>class</span><span> Page{ </span><span>public</span> <span>$firstRow</span>; <span>//</span><span> 起始行數(shù)</span> <span>public</span> <span>$listRows</span>; <span>//</span><span> 列表每頁顯示行數(shù)</span> <span>public</span> <span>$parameter</span>; <span>//</span><span> 分頁跳轉(zhuǎn)時要帶的參數(shù)</span> <span>public</span> <span>$totalRows</span>; <span>//</span><span> 總行數(shù)</span> <span>public</span> <span>$totalPages</span>; <span>//</span><span> 分頁總頁面數(shù)</span> <span>public</span> <span>$rollPage</span> = 11;<span>//</span><span> 分頁欄每頁顯示的頁數(shù)</span> <span>public</span> <span>$lastSuffix</span> = <span>true</span>; <span>//</span><span> 最后一頁是否顯示總頁數(shù)</span> <span>private</span> <span>$p</span> = 'p'; <span>//</span><span>分頁參數(shù)名</span> <span>private</span> <span>$url</span> = ''; <span>//</span><span>當(dāng)前鏈接URL</span> <span>private</span> <span>$nowPage</span> = 1<span>; </span><span>//</span><span> 分頁顯示定制</span> <span>private</span> <span>$config</span> = <span>array</span><span>( </span>'header' => '<span class="rows">共 %TOTAL_ROW% 條記錄</span>', 'prev' => '<<', 'next' => '>>', 'first' => '1...', 'last' => '...%TOTAL_PAGE%', 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',<span> ); </span><span>/*</span><span>* * 架構(gòu)函數(shù) * @param array $totalRows 總的記錄數(shù) * @param array $listRows 每頁顯示記錄數(shù) * @param array $parameter 分頁跳轉(zhuǎn)的參數(shù) </span><span>*/</span> <span>public</span> <span>function</span> __construct(<span>$totalRows</span>, <span>$listRows</span>=20, <span>$parameter</span> = <span>array</span><span>()) { C(</span>'VAR_PAGE') && <span>$this</span>->p = C('VAR_PAGE'); <span>//</span><span>設(shè)置分頁參數(shù)名稱</span> <span>/*</span><span> 基礎(chǔ)設(shè)置 </span><span>*/</span> <span>$this</span>->totalRows = <span>$totalRows</span>; <span>//</span><span>設(shè)置總記錄數(shù)</span> <span>$this</span>->listRows = <span>$listRows</span>; <span>//</span><span>設(shè)置每頁顯示行數(shù)</span> <span>$this</span>->parameter = <span>empty</span>(<span>$parameter</span>) ? <span>$_GET</span> : <span>$parameter</span><span>; </span><span>$this</span>->nowPage = <span>empty</span>(<span>$_GET</span>[<span>$this</span>->p]) ? 1 : <span>intval</span>(<span>$_GET</span>[<span>$this</span>-><span>p]); </span><span>$this</span>->nowPage = <span>$this</span>->nowPage>0 ? <span>$this</span>->nowPage : 1<span>; </span><span>$this</span>->firstRow = <span>$this</span>->listRows * (<span>$this</span>->nowPage - 1<span>); } </span><span>/*</span><span>* * 定制分頁鏈接設(shè)置 * @param string $name 設(shè)置名稱 * @param string $value 設(shè)置值 </span><span>*/</span> <span>public</span> <span>function</span> setConfig(<span>$name</span>,<span>$value</span><span>) { </span><span>if</span>(<span>isset</span>(<span>$this</span>->config[<span>$name</span><span>])) { </span><span>$this</span>->config[<span>$name</span>] = <span>$value</span><span>; } } </span><span>/*</span><span>* * 生成鏈接URL * @param integer $page 頁碼 * @return string </span><span>*/</span> <span>private</span> <span>function</span> url(<span>$page</span><span>){ </span><span>return</span> <span>str_replace</span>(<span>urlencode</span>('[PAGE]'), <span>$page</span>, <span>$this</span>-><span>url); } </span><span>/*</span><span>* * 組裝分頁鏈接 * @return string </span><span>*/</span> <span>public</span> <span>function</span><span> show() { </span><span>if</span>(0 == <span>$this</span>->totalRows) <span>return</span> ''<span>; </span><span>/*</span><span> 生成URL </span><span>*/</span> <span>$this</span>->parameter[<span>$this</span>->p] = '[PAGE]'<span>; </span><span>$this</span>->url = U(ACTION_NAME, <span>$this</span>-><span>parameter); </span><span>/*</span><span> 計算分頁信息 </span><span>*/</span> <span>$this</span>->totalPages = <span>ceil</span>(<span>$this</span>->totalRows / <span>$this</span>->listRows); <span>//</span><span>總頁數(shù)</span> <span>if</span>(!<span>empty</span>(<span>$this</span>->totalPages) && <span>$this</span>->nowPage > <span>$this</span>-><span>totalPages) { </span><span>$this</span>->nowPage = <span>$this</span>-><span>totalPages; } </span><span>/*</span><span> 計算分頁臨時變量 </span><span>*/</span> <span>$now_cool_page</span> = <span>$this</span>->rollPage/2<span>; </span><span>$now_cool_page_ceil</span> = <span>ceil</span>(<span>$now_cool_page</span><span>); </span><span>$this</span>->lastSuffix && <span>$this</span>->config['last'] = <span>$this</span>-><span>totalPages; </span><span>//</span><span>上一頁</span> <span>$up_row</span> = <span>$this</span>->nowPage - 1<span>; </span><span>$up_page</span> = <span>$up_row</span> > 0 ? '<a class="prev" href="' . <span>$this</span>->url(<span>$up_row</span>) . '">' . <span>$this</span>->config['prev'] . '</a>' : ''<span>; </span><span>//</span><span>下一頁</span> <span>$down_row</span> = <span>$this</span>->nowPage + 1<span>; </span><span>$down_page</span> = (<span>$down_row</span> <= <span>$this</span>->totalPages) ? '<a class="next" href="' . <span>$this</span>->url(<span>$down_row</span>) . '">' . <span>$this</span>->config['next'] . '</a>' : ''<span>; </span><span>//</span><span>第一頁</span> <span>$the_first</span> = ''<span>; </span><span>if</span>(<span>$this</span>->totalPages > <span>$this</span>->rollPage && (<span>$this</span>->nowPage - <span>$now_cool_page</span>) >= 1<span>){ </span><span>$the_first</span> = '<a class="first" href="' . <span>$this</span>->url(1) . '">' . <span>$this</span>->config['first'] . '</a>'<span>; } </span><span>//</span><span>最后一頁</span> <span>$the_end</span> = ''<span>; </span><span>if</span>(<span>$this</span>->totalPages > <span>$this</span>->rollPage && (<span>$this</span>->nowPage + <span>$now_cool_page</span>) < <span>$this</span>-><span>totalPages){ </span><span>$the_end</span> = '<a class="end" href="' . <span>$this</span>->url(<span>$this</span>->totalPages) . '">' . <span>$this</span>->config['last'] . '</a>'<span>; } </span><span>//</span><span>數(shù)字連接</span> <span>$link_page</span> = ""<span>; </span><span>for</span>(<span>$i</span> = 1; <span>$i</span> <= <span>$this</span>->rollPage; <span>$i</span>++<span>){ </span><span>if</span>((<span>$this</span>->nowPage - <span>$now_cool_page</span>) <= 0<span> ){ </span><span>$page</span> = <span>$i</span><span>; }</span><span>elseif</span>((<span>$this</span>->nowPage + <span>$now_cool_page</span> - 1) >= <span>$this</span>-><span>totalPages){ </span><span>$page</span> = <span>$this</span>->totalPages - <span>$this</span>->rollPage + <span>$i</span><span>; }</span><span>else</span><span>{ </span><span>$page</span> = <span>$this</span>->nowPage - <span>$now_cool_page_ceil</span> + <span>$i</span><span>; } </span><span>if</span>(<span>$page</span> > 0 && <span>$page</span> != <span>$this</span>-><span>nowPage){ </span><span>if</span>(<span>$page</span> <= <span>$this</span>-><span>totalPages){ </span><span>$link_page</span> .= '<a class="num" href="' . <span>$this</span>->url(<span>$page</span>) . '">' . <span>$page</span> . '</a>'<span>; }</span><span>else</span><span>{ </span><span>break</span><span>; } }</span><span>else</span><span>{ </span><span>if</span>(<span>$page</span> > 0 && <span>$this</span>->totalPages != 1<span>){ </span><span>$link_page</span> .= '<span class="current">' . <span>$page</span> . '</span>'<span>; } } } </span><span>//</span><span>替換分頁內(nèi)容</span> <span>$page_str</span> = <span>str_replace</span><span>( </span><span>array</span>('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'), <span>array</span>(<span>$this</span>->config['header'], <span>$this</span>->nowPage, <span>$up_page</span>, <span>$down_page</span>, <span>$the_first</span>, <span>$link_page</span>, <span>$the_end</span>, <span>$this</span>->totalRows, <span>$this</span>->totalPages), <span>$this</span>->config['theme'<span>]); </span><span>return</span> "<div>{<span>$page_str</span>}</div>"<span>; } }</span>
Usually, the data set is paging after data query. ThinkPHP also provides paging class to support data paging. Below are two examples of data paging.
The first one: using Page class and limit method
<span>$User</span> = M('User'); <span>//</span><span> 實(shí)例化User對象</span> <span>$count</span> = <span>$User</span>->where('status=1')-><span>count</span>();<span>//</span><span> 查詢滿足要求的總記錄數(shù)</span> <span>$Page</span> = <span>new</span> \Think\Page(<span>$count</span>,25);<span>//</span><span> 實(shí)例化Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial 傳入總記錄數(shù)和每頁顯示的記錄數(shù)(25)</span> <span>$show</span> = <span>$Page</span>->show();<span>//</span><span> 分頁顯示輸出 // 進(jìn)行分頁數(shù)據(jù)查詢 注意limit方法的參數(shù)要使用Page類的屬性</span> <span>$list</span> = <span>$User</span>->where('status=1')->order('create_time')->limit(<span>$Page</span>->firstRow.','.<span>$Page</span>->listRows)-><span>select(); </span><span>$this</span>->assign('list',<span>$list</span>);<span>//</span><span> 賦值數(shù)據(jù)集</span> <span>$this</span>->assign('page',<span>$show</span>);<span>//</span><span> 賦值分頁輸出</span> <span>$this</span>->display(); <span>//</span><span> 輸出模板</span>
Second type: Implementation of paging class and page method
<span>$User</span> = M('User'); <span>//</span><span> 實(shí)例化User對象 // 進(jìn)行分頁數(shù)據(jù)查詢 注意page方法的參數(shù)的前面部分是當(dāng)前的頁數(shù)使用 $_GET[p]獲取</span> <span>$list</span> = <span>$User</span>->where('status=1')->order('create_time')->page(<span>$_GET</span>['p'].',25')-><span>select(); </span><span>$this</span>->assign('list',<span>$list</span>);<span>//</span><span> 賦值數(shù)據(jù)集</span> <span>$count</span> = <span>$User</span>->where('status=1')-><span>count</span>();<span>//</span><span> 查詢滿足要求的總記錄數(shù)</span> <span>$Page</span> = <span>new</span> \Think\Page(<span>$count</span>,25);<span>//</span><span> 實(shí)例化Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial 傳入總記錄數(shù)和每頁顯示的記錄數(shù)</span> <span>$show</span> = <span>$Page</span>->show();<span>//</span><span> 分頁顯示輸出</span> <span>$this</span>->assign('page',<span>$show</span>);<span>//</span><span> 賦值分頁輸出</span> <span>$this</span>->display(); <span>//</span><span> 輸出模板</span>
Introduce query conditions
If it is a POST query, how to ensure that the original query conditions can be maintained after paging? We can pass in parameters to the paging class by assigning a value to the parameter attribute of the paging class
<span>$count</span> = <span>$User</span>->where(<span>$map</span>)-><span>count</span>();<span>//</span><span> 查詢滿足要求的總記錄數(shù)</span> <span>$Page</span> = <span>new</span> \Think\Page(<span>$count</span>,25);<span>//</span><span> 實(shí)例化Issues related to the use of ThinkPHP paging class, thinkphp paging_PHP tutorial 傳入總記錄數(shù)和每頁顯示的記錄數(shù) //分頁跳轉(zhuǎn)的時候保證查詢條件</span> <span>foreach</span>(<span>$map</span> <span>as</span> <span>$key</span>=><span>$val</span><span>) { </span><span>$Page</span>->parameter[<span>$key</span>] = <span>urlencode</span>(<span>$val</span><span>); } </span><span>$show</span> = <span>$Page</span>->show();<span>//</span><span> 分頁顯示輸出</span>
Pagination style customization
We can customize the output paging style. The paging class Page provides a setConfig method to modify some default settings. For example:
<span>$page</span>->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>條記錄 第<b>%NOW_PAGE%</b>頁/共<b>%TOTAL_PAGE%</b>頁</li>'<span>); </span><span>$page</span>->setConfig('prev', '上一頁'<span>); </span><span>$page</span>->setConfig('next', '下一頁'<span>); </span><span>$page</span>->setConfig('first', '首頁'<span>); </span><span>$page</span>->setConfig('last', '尾頁'<span>); </span><span>$page</span>->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
The attributes supported by the setConfig method include:
header: header description information, default value "Total %TOTAL_ROW% records"
next: next page description information, default value “>>”
first: description information on the first page, default value “1…”
last: last page description information, default value “…%TOTAL_PAGE%”
theme: paging theme description information, including a combination of all the above elements. Setting this attribute can change the display position of each unit of paging. The default value is "%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%"
Among them, the corresponding relationship between the display positions is:
Location Description
%FIRST% indicates the link display on the first page
%UP_PAGE% means the link to the previous page is displayed
%LINK_PAGE% indicates paginated link display
%DOWN_PAGE% indicates the link to the next page
%END% indicates the link display of the last page
In addition to changing the display information, you can also use styles to define the display effect of pagination. These style classes include: first (first page), prev (previous page), next (next page), end (last page), num (number of other pages), current (current page).
The ThinkPHP system paging class is already very complete. Just spend a few minutes studying the source code, and the official comments are also very clear. Here are some notes on problems in use. The system is set by default
<code class="hljs cs has-numbering"><span class="hljs-keyword">public $rollPage = <span class="hljs-number">11;<span class="hljs-comment">// 分頁欄每頁顯示的頁數(shù) <span class="hljs-keyword">public $lastSuffix = <span class="hljs-keyword">true; <span class="hljs-comment">// 最后一頁是否顯示總頁數(shù)</span></span></span></span></span></span></code><br /><br />
It’s not very clear from the comments at first. In fact, the first parameter is to control how many pages are displayed. If 11 page numbers are displayed by default, it is usually too long. I personally like to set it to 5. Of course, do not modify these parameters in the system source file. To modify it, just set $page->rollPage=5
when using it.
The second parameter is more confusing. When we display personalized paging, we usually use setConfig('last','尾頁');
to set the last page to be displayed as the "last page", but you can see tp in the show method. When $this->lastSuffix
is true, reset the value of the $this->config['last']
to the total number of pages. This will cause the last page to be displayed as a number no matter how you set it. I don’t know why the official settings are like this. The solution is still to reset $page->lastSuffix = false;
.

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.
