thinkphp實現數組分頁_PHP教程

來源:互聯網
上載者:User

在thinkphp的架構下實現分頁。公司的網站基於Thinkphp架構,一直久聞thinkphp的大名,終於有機會實戰了。thinkphp是MVC架構的,MVC對於任何ITers來說都不陌生,模型(model)-視圖(view)-控制器(controller)。他將邏輯和資料分開處理,少了很多繁瑣的過程。其實在官方的資料中已經詳細的介紹了怎麼分頁,傳送門:http://document.thinkphp.cn/manual_3_2.html#data_page

可是並不適用於資料已經從DB中取出,並且轉換為數組的情況,我接觸PHP滿打滿算2個月,接觸thinkphp不過3周。之前把很多時間花在了官方文檔上,去熟悉thinkphp。也算是磨刀不誤砍柴工吧。這裡把官方文檔當作比較進行闡述:
(只舉文檔上第一個方法):利用Page類和limit方法,代碼如下:

 = M('User'); = ->where('status=1')->(); =  \Think\Page(,25); = ->show(); = ->where('status=1')->order('create_time')->limit(->firstRow.','.->listRows)->->assign('list',);->assign('page',);->display(); 

用到了php內建的函數array_slice( )。定義在此:http://www.php.net/manual/en/function.array-slice.php
其實就是數組版的limit方法。好了,工具找到了,實現就很容易了。直接上代碼:

  =  = ->getNodeLists(-> = (['data' =  Page(,10 = (['data'], ->firstRow,-> = ->->assign('page',->assign('nodes',->

第3行getNodeLists方法從資料庫中取出資料並賦值給數組nodelist。
第5行count計算出數組元素的個數。
第6行為Page類傳入參數。
第7行的array_slice函數代替了limit方法。原理相同。
第9行用assign方法為模版賦值。定義在此:http://document.thinkphp.cn/manual_3_2.html#assign
第10行同理。

view中的代碼:

="page-list">

 = 'header' => '共 %TOTAL_ROW% 條記錄','prev' => '上一頁','next' => '下一頁','first'=> '第一頁','last' => '...%TOTAL_PAGE%','theme'=> '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',

http://www.bkjia.com/PHPjc/754073.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/754073.htmlTechArticle在thinkphp的架構下實現分頁。公司的網站基於Thinkphp架構,一直久聞thinkphp的大名,終於有機會實戰了。thinkphp是MVC架構的,MVC對於任何ITer...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.