本文實例講述了ThinkPHP3.2框架自帶分頁功能實現方法。分享給大家供大家參考,具體如下:
1.前端-分頁代碼:
<tfoot><!--分頁顯示?--><tr> <td textalign="center" cl nowrap="true" colspan="9" height="20"> <div class="pages">{$page}</div> </td></tr></tfoot>
2.創建分頁樣式:如page.css 并將以下代碼復制到該文件中
.pages{float: right}.pages a,.pages span { display:inline-block; padding:2px 10px; border:1px solid #f0f0f0; -webkit-border-radius:3px; -moz-border-radius:3px; border-radius:3px; font-size: 14px;}.pages a,.pages li { display:inline-block; list-style: none; text-decoration:none; color:#58A0D3;}.pages a.first,.pages a.prev,.pages a.next,.pages a.end{ margin:0 auto;}.pages a:hover{ border-color:#50A8E6;}.pages span.current{ background:#50A8E6; color:#FFF; font-weight:700; border-color:#50A8E6;}
3.前端頁面引入分頁樣式css文件
4.控制器中編寫index方法,將數據顯示到模板
方法(一):利用Page類和limit方法分頁
<?phpnamespace Admin/Controller;use Think/Controller;class DocController extends Controller{ function index(){ //實例化Doc數據表模型 $doc = D('Doc'); //調用count方法查詢要顯示的數據總記錄數 $count = $doc->count(); //echo $count;die; $page = new /Think/Page($count,2); // 分頁顯示輸出 $show = $page->show(); $this->assign('page',$show); // 進行分頁數據查詢 注意limit方法的參數要使用Page類的屬性 $doc_list = $doc->limit($page->firstRow.','.$page->listRows)->select(); $this->assign('doc_list',$doc_list); $this->display(); }
方法(二):分頁類和page方法的實現分頁
<?phpnamespace Admin/Controller;use Think/Controller;class DocController extends Controller{ function index(){ //實例化Doc數據表模型 $doc = D('Doc'); //進行分頁數據查詢 注意page方法的參數的前面部分是當前的頁數使用 $_GET[p]獲取 $doc_list = $doc->page($_GET['p'] . ',2')->select(); $this->assign('doc_list', $doc_list);// 賦值數據集 $count = $doc->count();// 查詢滿足要求的總記錄數 $page = new /Think/Page($count, 2);// 實例化分頁類 傳入總記錄數和每頁顯示的記錄數 $show = $page->show();// 分頁顯示輸出 $this->assign('page', $show);// 賦值分頁輸出 $this->display(); // 輸出模板 }
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選