Paging is implemented under the thinkphp framework. The company's website is based on the Thinkphp framework. I have been hearing about thinkphp for a long time and finally had the opportunity to put it into practice. thinkphp is based on MVC architecture. MVC is familiar to any ITers, model-view-controller. It handles logic and data separately, eliminating a lot of tedious processes. In fact, the official information has introduced in detail how to paginate, portal: http://document.thinkphp.cn/manual_3_2.html#data_page
However, it does not apply to the case where the data has been taken out from the DB and converted into an array. I have been exposed to PHP for a full 2 months, and I have been exposed to thinkphp for only 3 weeks. I spent a lot of time on official documents to get familiar with thinkphp. It can be regarded as a worker who sharpens his sword and chops firewood. The official document is explained here as a comparison:
(only the first method in the document is cited): Using the Page class and the limit method, the code is as follows:
<span $User</span> = M('User'); <span //</span><span 实例化User对象</span> <span $count</span>= <span $User</span>->where('status=1')-><span count</span>();<span //</span><span 查询满足要求的总记录数</span> <span $Page</span> = <span new</span> \Think\Page(<span $count</span>,25);<span //</span><span 实例化分页类 传入总记录数和每页显示的记录数(25)</span> <span $show</span> = <span $Page</span>->show();<span //</span><span 分页显示输出 // 进行分页数据查询 注意limit方法的参数要使用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 赋值数据集</span> <span $this</span>->assign('page',<span $show</span>);<span //</span><span 赋值分页输出</span> <span $this</span>->display(); <span //</span><span 输出模板</span>
The function array_slice( ) that comes with PHP is used. The definition is here: http://www.php.net/manual/en/function.array-slice.php
is actually the array version of the limit method. Well, now that the tool is found, implementation is easy. Directly upload the code:
<span public</span> <span function</span><span nodeslist(){ </span><span $portal</span> = <span new</span><span PortalApi; </span><span $nodelist</span> = <span $portal</span>->getNodeLists(<span $this</span>-><span uid); </span><span $count</span> = <span count</span>(<span $nodelist</span>['data'<span ]); </span><span $p</span> = <span new</span> Page(<span $count</span>,10<span ); </span><span $lists</span> = <span array_slice</span>(<span $nodelist</span>['data'], <span $p</span>->firstRow,<span $p</span>-><span listRows); </span><span $page</span> = <span $p</span>-><span show(); </span><span $this</span>->assign('page',<span $page</span><span ); </span><span $this</span>->assign('nodes',<span $lists</span><span ); </span><span $this</span>-><span display(); }</span>
The getNodeLists method in line 3 retrieves data from the database and assigns it to the array nodelist.
Line 5 count calculates the number of array elements.
Line 6 passes in parameters to the Page class.
The array_slice function on line 7 replaces the limit method. The principle is the same.
Line 9 uses the assign method to assign a value to the template. The definition is here: http://document.thinkphp.cn/manual_3_2.html#assign
The same applies to line 10.
Code in view:
<div <span class</span>="page-list"><span {</span><span $page</span><span } </span></div>
<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'=> '第一页', 'last' => '...%TOTAL_PAGE%', 'theme'=> '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',