An example of how to implement the paging function of thinkphp3.2 framework

Time:2019-11-6

This paper gives an example of how to realize the paging function of thinkphp3.2 framework. To share with you for your reference, as follows:

1. Front end – paging code:

<tfoot>
<! -- display by page? >
<tr>
  <td textalign="center" cl nowrap="true" colspan="9" height="20">
   <div>{$page}</div>
  </td>
</tr>
</tfoot>

2. Create a paging style, such as page.css, and copy the following code to the file


.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. Introduce paging style CSS file to front page

4. Write index method in the controller to display the data to the template

Method (1): paging with page class and limit method

<?php
namespace Admin\Controller;
use Think\Controller;
class DocController extends Controller{
  function index(){
    //Instantiate doc data table model
    $doc = D('Doc');
    //Call the count method to query the total number of data records to display
    $count = $doc->count();
    //echo $count;die;
    $page = new \Think\Page($count,2);
    //Paging display output
    $show = $page->show();
    $this->assign('page',$show);
    //For paging data query, note that the parameter of limit method should use the property of page class
    $doc_list = $doc->limit($page->firstRow.','.$page->listRows)->select();
    $this->assign('doc_list',$doc_list);
    $this->display();
  }

Method (2): implementation of paging class and page method

<?php
namespace Admin\Controller;
use Think\Controller;
class DocController extends Controller{
  function index(){
    //Instantiate doc data table model
    $doc = D('Doc');
    //For paging data query, note that the first part of the parameters of the page method is the current page number, which is obtained by $_get [P]
    $doc_list = $doc->page($_GET['p'] . ',2')->select();
    $this - > assign ('doc list ', $doc list); // assign dataset
    $count = $doc - > count(); // query the total number of records that meet the requirements
    $page = new \ think \ page ($count, 2); // the total number of incoming records of instantiated page class and the number of records displayed per page
    $show = $page - > show(); // page display output
    $this - > assign ('page ', $show); // assign page output
    $this - > display(); // output template
  }

For more information about ThinkPHP, readers who are interested in it can see the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, advanced tutorial of CI (CodeIgniter) framework, introduction to Zend framework and technical summary of PHP template.

I hope that this article will be helpful for you to design your PHP program based on the think PHP framework.