The most detailed ThinkPHP5 custom paging class tutorial

Source: Internet
Author: User
Tags sprintf
As a PHP programmer, when writing a list-nature page, almost all of them write a paging program. There are many ways of paging, this article we will give you a detailed introduction to the ThinkPHP5 custom paging class tutorial.

The first step: Create a file page.php, put it to extend\page, here you can decide, the namespace is right on the line

<?phpnamespace page;//+----------------------------------------------------------------------//| thinkphp [WE CAN do IT JUST THINK]//+----------------------------------------------------------------------//| Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.//+-------------------------------------------------- --------------------// | Licensed (http://www.apache.org/licenses/LICENSE-2.0)//+------------------------------------------------------- ---------------// | Author:zhangyajun <448901948@qq.com>//+------------------------------------------------------------------ ----use think\paginator;            Class Page extends paginator{//home protected function Home () {if ($this->currentpage () > 1) { Return "<a href= '". $this->url (1).        "' title= ' Home > Home </a>";        } else {return "<p> home </p>";         }}//previous page protected function prev () {if ($this->currentpage () > 1) {   Return "<a href= '". $this->url ($this->currentpage-1).        "' title= ' prev > prev </a>";        } else {return ' <p> prev </p> '; }}//Next page protected function next () {if ($this->hasmore) {return "<a href= '". $this-& Gt;url ($this->currentpage + 1).        "' title= ' next page ' > Next </a>";        } else {return ' <p> next </p> '; }}//End protected function last () {if ($this->hasmore) {return "<a href= '". $this-&G T;url ($this->lastpage).        "' title= ' last ' > Last </a>";        } else {return ' <p> last </p> '; }}//Statistics protected function info () {return "<p class= ' Pageremark ' > Total <b>". $this->lastpag            E. "</b> page <b>". $this->total.    "</b> data </p>";        }/** * Page Number button * @return String */protected function getlinks () {$block = [    ' First ' = null, ' slider ' = null, ' last ' = null];        $side = 3;         $window = $side * 2;        if ($this->lastpage < $window + 6) {$block [' first '] = $this->geturlrange (1, $this->lastpage);            } elseif ($this->currentpage <= $window) {$block [' first '] = $this->geturlrange (1, $window + 2);        $block [' last '] = $this->geturlrange ($this->lastpage-1, $this->lastpage); } elseif ($this->currentpage > ($this->lastpage-$window)) {$block [' first '] = $this->geturlrange (            1, 2);        $block [' last '] = $this->geturlrange ($this->lastpage-($window + 2), $this->lastpage);            } else {$block [' first '] = $this->geturlrange (1, 2);            $block [' slider '] = $this->geturlrange ($this->currentpage-$side, $this->currentpage + $side); $block [' last '] = $this->geturlrange ($this->lastpage-1, $this->lastpage);         } $html = ';        if (Is_array ($block [' first ')]) {$html. = $this->geturllinks ($block [' first ']);            } if (Is_array ($block [' Slider ')]) {$html. = $this->getdots ();        $html. = $this->geturllinks ($block [' Slider ']);            } if (Is_array ($block [' last ')]) {$html. = $this->getdots ();        $html. = $this->geturllinks ($block [' last ']);    } return $html;            }/** * Renders the paging HTML * @return Mixed */Public function render () {if ($this->haspages ()) { if ($this->simple) {return sprintf ('%s<div class= "pagination" >%s%s %s</div> ', $this->css (), $this->prev (), $this->get            Links (), $this->next ()); } else {return sprintf ('%s<divclass= "pagination" >%s%s%s%s%s</div> ', $this->css (), $this->hom                    E (), $this->prev (), $this->getlinks (), $this->next (),            $this->last (), $this->info ());     }}}/** * generates a clickable button * * @param string $url * @param int $page * @return string */protected function Getavailablepagewrapper ($url, $page) {return ' <a href= "'. Htmlentities ($url). ' "title=". $page. ' " Page ">". $page.    ' </a> '; /** * Generates a disabled button * * @param string $text * @return String */protected function Getdisabledte Xtwrapper ($text) {return ' <p class= ' pageellipsis ' > '. $text.    ' </p> '; /** * Generates an active button * * @param string $text * @return String */protected function GetActivePage Wrapper ($texT) {return ' <a href= ' "class=" cur ">". $text.    ' </a> '; }/** * Generate ellipsis button * * @return String */protected function getdots () {return $this->get    Disabledtextwrapper (' ... ');     }/** * Generate page number buttons in bulk.  * * @param array $urls * @return String */protected function geturllinks (array $urls) {$html =         '';        foreach ($urls as $page = = $url) {$html. = $this->getpagelinkwrapper ($url, $page);    } return $html; /** * Generate normal page Number button * * @param string $url * @param int $page * @return String */Protect ed function Getpagelinkwrapper ($url, $page) {if ($page = = $this->currentpage ()) {return $this-&G        T;getactivepagewrapper ($page);    } return $this->getavailablepagewrapper ($url, $page); }/** * Page style */protected function css () {return ' <style type= ' text/css ' >.Pagination p{margin:0;                Cursor:pointer}. pagination{height:40px;            padding:20px 0px;                }. pagination a{Display:block;                Float:left;                margin-right:10px;                PADDING:2PX 12px;                height:24px;                border:1px #cccccc Solid;                Background: #fff;                Text-decoration:none;                Color: #808080;                font-size:12px;            line-height:24px;                }. Pagination a:hover{color: #077ee3;                Background:white;            border:1px #077ee3 Solid;                }. pagination a.cur{Border:none;                Background: #077ee3;            Color: #fff;                }. pagination p{float:left;                PADDING:2PX 12px;                font-size:12px; height:24pX                line-height:24px;                Color: #bbb;                border:1px #ccc Solid;                Background: #fcfcfc;             margin-right:8px;                }. pagination p.pageremark{Border-style:none;                Background:none;                margin-right:0px;                PADDING:4PX 0px;            Color: #666;            }. pagination P.pageremark b{color:red;                }. pagination p.pageellipsis{Border-style:none;                Background:none;                PADDING:4PX 0px;            Color: #808080;    }. Dates Li {font-size:14px;margin:20px 0}. Dates li span{float:right} </style> '; }}

Step Two: Modify the configuration file

Paging configuration     ' paginate '               = [         ' type ' = ' = '      page\page ',//Pagination class         ' var_page ' = ' page  ',         ' List_rows ' = +,     ],

Preview:

The above is thinkPHP5.0 custom page class tutorial in detail, we can do one's own, hope to help everyone.

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.