Home  >  Article  >  Backend Development  >  ThinkPHP data paging Page.class.php_PHP tutorial

ThinkPHP data paging Page.class.php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:22:261032browse

ThinkPHP data paging Page.class.php

Get pagination class

ThinkPHP provides an extension class library Page for data paging, which can be downloaded at http://www.thinkphp.cn/extend/241.html, or download the official complete extension package (http://www.thinkphp.cn/down/ 253.html) also contains the paging extension class. Place the decompressed Page.class.php under the ThinkPHP/Extend/Library/ORG/Util/ (please create it manually if it does not exist) directory.
Of course, the location of the extended class library is actually more arbitrary. You can also put it under the class library directory of the project. The difference is just in your import path.

Paginated query

The paging class needs to be combined with the query. We can use the limit method or the page method that comes with ThinkPHP. The purpose is to obtain the current paging data (there is also a method of obtaining the complete data first and then displaying it in front-end paging, which is not described in this article, nor suggestion). Using the limit method or page method has nothing to do with the database type.

We first create a think_data data table in the database for testing:

    CREATE TABLE IF NOT EXISTS `think_data` (
      `id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
      `title` varchar(255) NOT NULL,
      `content` varchar(255) NOT NULL,
      `create_time` int(11) unsigned NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;


Copy code To use paging query, generally speaking, two queries are required, that is, the first query is to obtain the total amount of data that meets the conditions, and then the second query is to query the current paging data. The function of this is to tell the paging class the current total number of data. In order to calculate the total number of pages generated (if your display only needs to page up and down, the total query can be omitted or cached).

A standard example of paging usage is as follows:

    $Data = M('Data'); // 实例化Data数据对象
    import('ORG.Util.Page');// 导入分页类
    $count      = $Data->where($map)->count();// 查询满足要求的总记录数 $map表示查询条件
    $Page       = new Page($count);// 实例化分页类 传入总记录数
    $show       = $Page->show();// 分页显示输出
    // 进行分页数据查询
    $list = $Data->where($map)->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
    $this->assign('list',$list);// 赋值数据集
    $this->assign('page',$show);// 赋值分页输出
    $this->display(); // 输出模板


Copy code

If there is no data, the pagination will be blank. So before testing, please make sure that your data table has certain data, otherwise you may not see the paging effect. If you use the page method to query, you can change it to

    $Data = M('Data'); // 实例化Data数据对象
    import('ORG.Util.Page');// 导入分页类
    $count      = $Data->where($map)->count();// 查询满足要求的总记录数
    $Page       = new Page($count);// 实例化分页类 传入总记录数
    // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
    $nowPage = isset($_GET['p'])?$_GET['p']:1;
    $list = $Data->where($map)->order('create_time')->page($nowPage.','.$Page->listRows)->select();
    $show       = $Page->show();// 分页显示输出
    $this->assign('page',$show);// 赋值分页输出
    $this->assign('list',$list);// 赋值数据集
    $this->display(); // 输出模板



Copy code

Then, we can add the paging output variable in the template


<table cellpadding=3 cellspacing=5>
 <volist name="list" id="vo">
 <tr>
 <td >[ {$vo.create_time|date=&#39;Y-m-d H:i:s&#39;,###} ] {$vo.title} </td>
 </tr>
 </volist>
 <tr>        
 </tr>
 </table>
 <div class="result page">{$page}</div>



Copy code You can see that paging output only needs to use the {$page} variable to output in the template.

Pagination settings

Set paging variables

By default, the variable passed in paging is p, and the generated paging jump address may be similar to:
  1. http://serverName/index.php/Data/index/p/1
  2. http://serverName/index.php/Data/index/p/2 Copy code We can configure the VAR_PAGE configuration parameter to change:
    1. 'VAR_PAGE'=>'page' Copy code Then the paging address becomes:
      1. http://serverName/index.php/Data/index/page/1
      2. http://serverName/index.php/Data/index/page/1 Copy code

        Set the number of records per page

        By default, each page of paging display will display 20 pieces of data. If you want to change the amount of data displayed on each page, you can pass the second parameter when instantiating the paging class:
        1. $Page = new Page($count,5);// Instantiate the paging class, pass in the total number of records and display 5 records per page Copy code Since we use the $Page->listRows attribute in the query method, there is no need to change it, but if you use numbers directly in the query method, please remember to change it together.
          The following is the display effect of the official paging example:

          Pass in paging conditions

          By default, the paging class will automatically obtain the POST (first) or GET variable of the current page as the passed value for the paging jump. If you need to specify the parameters for the current paging jump, you can set the parameter attribute. The parameter attribute supports 2 Pass values ​​in three ways: strings and arrays. The string adopts the format of var1=val1&var2=val2..., for example:
          1. foreach($map as $key=>$val) {
          2. $Page->parameter .= "$key=".urlencode($val).'&';
          3. } Copy code Or pass in the array directly:
            1. $Page->parameter = array_map('urlencode',$map); Copy code Since the U function is called internally, the paging jump link finally generated by the paging class will automatically generate an address consistent with the current URL pattern based on the current URL settings, so there is no need to worry about the parameters of the paging link affecting the URL address.

              Pagination routing support

              If your paging jump link address uses routing, you can set the url parameters. For example, suppose our paging URL address format is:
              1. http://serverName/data/index/1
              2. http://serverName/data/index/2
              3. http://serverName/data/index/3 Copy code Such a URL routing address, then we can set
                1. $Page->url = 'data/index'; Copy code After setting, the link address of the paging class will automatically generate the above URL format address.
                  Note that if the url parameter and parameter are used at the same time, the latter is invalid.

                  Set the number of pages displayed

                  You can set related properties after instantiating the paging class. By default, the number of pages displayed on the page is 5, we can modify it:
                  1. $Page->rollPage = 3; Copy code In this way, only 3 paginations can be seen on the page at the same time

                    Pagination display customization

                    The above talks about the parameter settings of paging. Next, we will talk about how to set the paging display effect (including style). The default paging effect may not meet all requirements. The paging class provides a setConfig method to modify some default settings.For example:
                    1. $page->setConfig('header','members'); Copy code The properties supported by the setConfig method include:
                      header 头部描述信息,默认值 “条记录”
                      prev 上一页描述信息,默认值是“上一页”
                      next 下一页描述信息,默认值是“下一页”
                      first 第一页描述信息,默认值是“第一页”
                      last 最后一页描述信息,默认值是“最后一页”
                      theme 分页主题描述信息,包括了上面所有元素的组合 ,设置该属性可以改变分页的各个单元的显示位置,默认值是
                      "%totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%"
                      Setting the above properties through setConfig can perfectly customize your paging display style.

                      www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/847866.htmlTechArticleThinkPHP data paging Page.class.php Get the paging class ThinkPHP provides an extension class library for data paging, which can be found in Download from http://www.thinkphp.cn/extend/241.html, or download the official complete...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn