thinkphp Page class usage related issues, thinkphp pagination _php Tutorial

Source: Internet
Author: User
Tags urlencode

thinkphp Page class usage related issues, thinkphp pagination


ThinkPHP3.2.3Page.class.php File Source
 Php// +----------------------------------------------------------------------// | thinkphp [WE CAN do it JUST THINK it]//+----------------------------------------------------------------------//| Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.//+-------------------------------------------------- --------------------// | Licensed (http://www.apache.org/licenses/LICENSE-2.0)//+------------------------------------------------------- ---------------// | Author: Mai when fuel 
   
    
     //+----------------------------------------------------------------------
     
  
 
    namespace Think;classpage{ Public $firstRow;//number of start lines     Public $listRows;//List of rows per page     Public $parameter;//parameters to take when paging jumps     Public $totalRows;//total number of rows     Public $totalPages;//number of page total pages     Public $rollPage= 11;//Number of pages displayed per page in the page bar     Public $lastSuffix=true;//whether the last page shows the total number of pages    Private $p= ' P ';//Paging parameter name    Private $url= '';//Current Link URL    Private $nowPage= 1; //Pagination Display Customization    Private $config=Array(        ' Header ' = 'Total%total_row% Records', ' prev ' = ' << ', ' next ' = ' >> ', ' first ' = ' 1 ... ', ' last ' ' ...%total_page% ', ' theme ' = '%first%%up_page%%link_page%%down_page% ',    ); /** * Schema function * @param array $totalRows total number of records * @param array $listRows shows the number of records per page * @param array $parameter Parameters for paging jumps*/     Public function__construct ($totalRows,$listRows=20,$parameter=Array()) {C (' Var_page ') &&$this->p = C (' var_page ');//Set paging parameter name        /*Basic Settings*/        $this->totalrows =$totalRows;//set total number of records        $this->listrows =$listRows;//set the number of rows to display per page        $this->parameter =Empty($parameter) ?$_get:$parameter; $this->nowpage =Empty($_get[$this->p])? 1:intval($_get[$this-p]); $this->nowpage =$this->nowpage>0?$this->nowpage:1; $this->firstrow =$this->listrows * ($this->nowpage-1); }    /** * Custom Paging link settings * @param string $name set name * @param string $value Set Value*/     Public functionSetconfig ($name,$value) {        if(isset($this->config[$name])) {            $this->config[$name] =$value; }    }    /** * Generate link URL * @param integer $page page number * @return string*/    Private functionUrl$page){        return Str_replace(UrlEncode(' [PAGE] '),$page,$this-URL); }    /** * Assemble page links * @return string*/     Public functionShow () {if(0 = =$this->totalrows)return''; /*Generate URL*/        $this->parameter[$this-&GT;P] = ' [PAGE] '; $this->url = U (Action_name,$this-parameter); /*Calculate Paging Information*/        $this->totalpages =Ceil($this->totalrows/$this->listrows);//Total Pages        if(!Empty($this->totalpages) &&$this->nowpage >$this-totalpages) {            $this->nowpage =$this-TotalPages; }        /*Calculate Paging Temp variable*/        $now _cool_page=$this->rollpage/2; $now _cool_page_ceil=Ceil($now _cool_page); $this->lastsuffix &&$this->config[' last '] =$this-TotalPages; //Previous Page        $up _row=$this->nowpage-1; $up _page=$up _row> 0? ' $this->url ($up _row) . ' > '.$this->config[' prev '. '' : ''; //Next Page        $down _row=$this->nowpage + 1; $down _page= ($down _row<=$this->totalpages)? ' $this->url ($down _row) . ' > '.$this->config[' Next '. '' : ''; //first Page        $the _first= ''; if($this->totalpages >$this->rollpage && ($this->nowpage-$now _cool_page) >= 1){            $the _first= ' $this->url (1). ' > '.$this->config[' first '. ''; }        //last page        $the _end= ''; if($this->totalpages >$this->rollpage && ($this->nowpage +$now _cool_page) <$this-totalpages) {            $the _end= ' $this->url ($this->totalpages). ' > '.$this->config[' last ']. ''; }        //Digital Connection        $link _page= "";  for($i= 1;$i<=$this->rollpage;$i++){            if(($this->nowpage-$now _cool_page) <= 0 ){                $page=$i; }ElseIf(($this->nowpage +$now _cool_page-1) >=$this-totalpages) {                $page=$this->totalpages-$this->rollpage +$i; }Else{                $page=$this->nowpage-$now _cool_page_ceil+$i; }            if($page> 0 &&$page!=$this-nowpage) {                if($page<=$this-totalpages) {                    $link _page. = ' $this->url ($page) . ' > '.$page. ''; }Else{                     Break; }            }Else{                if($page> 0 &&$this->totalpages! = 1){                    $link _page.= '' . $page . ''; }            }        }        //Replace paging content        $page _str=Str_replace(            Array('%header% ', '%now_page% ', '%up_page% ', '%down_page% ', '%first% ', '%link_page% ', '%end% ', '%total_row% ', '%total_page% ' '),Array($this->config[' header '],$this->nowpage,$up _page,$down _page,$the _first,$link _page,$the _end,$this->totalrows,$this->totalpages),$this->config[' Theme ']); return"{$page _str}"; }}

The data set is typically paged after a data query, and thinkphp provides paging classes to support data paging. Here are two examples of data paging.

First: Using the Page class and the Limit method

$User // instantiating a User object $count      $User->where (' Status=1 '),Count(); // the total number of records that the query meets the requirements $Page       new \think\page ($count, 25); // instantiate the total number of incoming records in a paging class and the number of records displayed per page $show       $Page->show (); // Paging display output//Paging data query Note the parameter of the limit method to use the properties of the page class $list $User->where (' Status=1 ')->order (' Create_time ')->limit ($Page->firstrow. ', '.  $Page->listrows), select (); $this->assign (' list ',$list); // Assignment Data Set $this->assign (' page ',$show); // Assignment Paging output $this // Output Template

Second: implementation of the paging class and the page method

$User // instantiate the User object//Paging data query Note the preceding part of the parameter of the page method is the current number of pages using $_get[p] Get $list $User->where (' Status=1 ')->order (' Create_time ')->page ($_get[' P '].  Select (); $this->assign (' list ',$list); // Assignment Data Set $count      $User->where (' Status=1 '),Count(); // the total number of records that the query meets the requirements $Page       new \think\page ($count, 25); // instantiate the number of incoming total records in a paging class and the number of records displayed per page $show       $Page->show (); // pagination Display Output $this->assign (' page ',$show); // Assignment Paging output $this // Output Template

Bring in query criteria
If it is a post query, how to ensure that after paging can maintain the original query conditions, we can give the paging class passed parameters, by assigning a value to the parameter property of the page class

$count      $User->where ($map),Count(); // the total number of records that the query meets the requirements $Page       new \think\page ($count, 25); // instantiate the total number of incoming records in a paging class and the number of records displayed per page//paging to ensure query conditions foreach ($mapas$key=$val) {    $Page parameter[$key]   =   urlencode($val);} $show       $Page->show (); // pagination Display Output

Pagination Style Customization
We can customize the paging style of the output, and the paging class page provides a Setconfig method to modify some of the default settings. For example:

$page->setconfig (' Header ', '
  • Total %total_row% records %now_page% page/Total %total_page% page
  • '); $page->setconfig (' prev ', ' prev '); $page->setconfig (' Next ', ' next page '); $page->setconfig (' first ', ' Home '); $page->setconfig (' Last ', ' End '); $page->setconfig (' theme ', '%first%%up_page%%link_page%%down_page%%end%%header% ');

    The properties supported by the Setconfig method include:
    Header: Header description information, default value "Total%total_row% record"
    Next: The next page describes the information, the default value of ">>"
    First: Describe the information on page one, default value "1 ..."
    Last: Final page description information, default value "...%total_page%"
    Theme: Pagination topic description information, including all of the above elements of the combination, set this property can change the pagination of each cell display location, the default value is "%first%%up_page%%link_page%%down_page%%end%"

    where the corresponding relationship of the display location is:
    Location description
    %first% indicates that the first page of the link appears
    %up_page% indicates that a link to the previous page shows
    %link_page% indicates that the paging link is displayed
    %down_page% indicates that the next page of the link appears
    %end% indicates that the last page of the link appears
    In addition to changing display information, you can also use styles to define the display of pagination. These style classes include: First (Page one), prev (previous), Next (next), End (last), Num (number of other pages), current page.

    thinkphp System the pagination class has been very well written. Just take a few minutes to study the source code, and the official notes are clear. Here is a comment on the problem in use. The system is set by default

    public $rollPage   = 11;// 分页栏每页显示的页数public $lastSuffix = true; // 最后一页是否显示总页数

    At the beginning of the note is not very clear, in fact, the first parameter is to control the number of pages displayed, if the default display 11 page number is generally too long, personal preference to set to 5, of course, modify these parameters do not change in the system source files, but in the use of the settings $page->rollPage=5 can be.

    The second parameter is more deceptive, we usually use to set the last page as "last" when we personalize the pagination display setConfig('last','尾页'); , but in the Show method we can see that TP $this->lastSuffix resets the $this->config['last'] The value for the total number of pages this causes no matter how you set the last industry to show as numbers. I do not know why the official set up, the solution is still reset $page->lastSuffix = false; .

    http://www.bkjia.com/PHPjc/1100149.html www.bkjia.com true http://www.bkjia.com/PHPjc/1100149.html techarticle thinkphp Page class usage related issues, thinkphp paging ThinkPHP3.2.3 page. class. php file source code? PHP//+--------------------------------------- ------------------------------...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.