Home  >  Article  >  PHP Framework  >  Implementation and precautions of paging page number jump function in ThinkPHP3.2

Implementation and precautions of paging page number jump function in ThinkPHP3.2

PHPz
PHPzOriginal
2023-04-17 09:48:54637browse

With the continuous development of modern Internet technology, the number of website visits is increasing. In order to facilitate users to access data, the paging function has become one of the essential functions of modern websites. In the ThinkPHP3.2 framework, the paging function is also very flexible, and you can customize the paging style and page number jump function. This article will introduce the implementation method and precautions of the paging page number jump function in ThinkPHP3.2.

1. Paging function of ThinkPHP3.2

In the ThinkPHP3.2 framework, simple paging function can be implemented through the following methods:

// 查询数据
$list = M('User')->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();

// 实例化分页类
$Page = new \Think\Page($count,$pageSize);

// 输出分页html
$this->assign('page',$Page->show());

Among them, $list is the query For the data obtained, $where is the query condition, $count is the total number of query data, and $pageSize is the amount of data displayed on each page. After instantiating the \Think\Page class, you can output paginated html through the $Page->show() method.

2. Customized paging style

In the ThinkPHP3.2 framework, you can customize the paging style through the following methods:

  1. Define paging style

Create a file named Page.class.php in the current project and copy the following code into the file:

<?php
namespace Think;
class Page {
    // 定义分页样式
    private $rollPage   = 5; // 分页栏每页显示的页数
    private $lastSuffix = true; // 是否显示最后一页的页码后缀
    private $config     = array(
        &#39;header&#39; => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
        'prev'   => '<<&#39;,
        &#39;next&#39;   => '>>',
        'first'  => '1...',
        'last'   => '...%TOTAL_PAGE%',
        'theme'  => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
    );
    private $totalRows  = 0; // 总行数
    private $listRows   = 20; // 每页显示行数
    private $totalPages = 0; // 总页数
    private $nowPage    = 1; // 当前页数
    private $firstRow   = 1; // 起始行数
    private $varPage    = 'page'; // 分页变量名
    private $p          = 'p'; // 分页参数名
    private $url        = ''; // 当前链接URL
    private $baseUrl    = ''; // 分页基础URL
    private $params     = array(); // 分页跳转时要带的参数
    private $anchor     = ''; // 锚点参数
    // 构造方法
    public function __construct($totalRows,$listRows='',$p='page'){
        $this->totalRows = $totalRows;
        $this->varPage = $p;
        if (!empty($listRows)) {
            $this->listRows = $listRows;
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows); //总页数
        $this->nowPage    = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow   = $this->listRows*($this->nowPage-1);
    }
    // 显示分页
    public function show($url='') {
        if (0 == $this->totalRows) {
            return '';
        }

        // 记录当前URL
        $this->url = empty($url) ? U(ACTION_NAME).'/' : $url;
        // 获取当前页码
        $this->nowPage    = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        // 计算分页信息
        $this->totalPages = ceil($this->totalRows/$this->listRows);

        // 分页处理
        if($this->totalPages > $this->rollPage){
            $linkPage = "<ul class=&#39;pagination&#39;>";
            $inum = floor($this->rollPage/2);
            if($this->nowPage <= $inum){
                $this->lastSuffix=false;
            }
            if($this->nowPage > 1){
                $this->addUrl($linkPage,$this->nowPage-1,'上一页','class="prev"');
            }
            for($i = 1; $i <= $this->rollPage; $i++){
                if(($this->nowPage+$inum) >= $this->totalPages && $this->totalPages > $this->rollPage){
                    $page = $this->totalPages-$this->rollPage+$i;
                }else{
                    $page = $this->nowPage-$inum+$i;
                }
                if($page > 0 && $page != $this->nowPage){
                    if($page <= $this->totalPages){
                        $this->addUrl($linkPage,$page,'第'.$page.'页','');
                    }else{
                        break;
                    }
                }else{
                    if($page > 0 && $this->rollPage < $this->totalPages){
                        $this->addUrl($linkPage,$page,'第'.$page.'页','');
                    }
                }
            }
            if($this->nowPage < $this->totalPages){
                $this->addUrl($linkPage,$this->nowPage+1,'下一页','class="next"');
            }
            if($this->lastSuffix){
                $this->addUrl($linkPage,$this->totalPages,'最后一页');
            }
            $linkPage.='</ul>';
        }else{
            $linkPage = "<ul class=&#39;pagination&#39;>";
            for($i=1;$i<=$this->totalPages;$i++){
                if($this->nowPage==$i) {
                    $linkPage.='<li class="active"><span>'.$i.'</span></li>';
                } else {
                    $this->addUrl($linkPage,$i,'第'.$i.'页','');
                }
            }
            $linkPage.='</ul>';
        }
        $pageStr  = str_replace(
            array('%HEADER%','%NOW_PAGE%','%TOTAL_PAGE%','%TOTAL_ROW%','%FIRST%','%UP_PAGE%','%LINK_PAGE%','%DOWN_PAGE%','%END%'),
            array($this->config['header'],$this->nowPage,$this->totalPages,$this->totalRows,$this->config['first'],$this->config['prev'],$linkPage,$this->config['next'],$this->config['last']),$this->config['theme']);
        return $pageStr;
    }
    // 添加分页URL
    protected function addUrl(&$linkPage,$page,$text,$class=''){
        if($page > 0){
            $url = $this->url;
            $url .= strpos($url,'?') ? '&' : '?';
            $url .= $this->p.'='.$page;
            if(!empty($this->params)){
                foreach($this->params as $key=>$val){
                    $url .= '&'.$key.'='.$val;
                }
            }
            if(!empty($this->anchor)){
                $url .= '#'.$this->anchor;
            }
            $linkPage.='<li &#39;.$class.&#39;><a href="&#39;.htmlentities($url).&#39;">'.$text.'</a></li>';
        }
    }
}
  1. Use custom paging style

Introduce a custom paging style into the controller and use it in the following way:

// 引入分页类
import("Think.Page");
// 查询数据总数
$count = M('User')->where($where)->count();
// 实例化分页类
$Page = new \Think\Page($count,$pageSize);
// 自定义分页样式
$Page->setConfig('prev', '上一页');
$Page->setConfig('next', '下一页');
$Page->setConfig('first', '第一页');
$Page->setConfig('last', '最后一页');
$Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
// 查询数据
$list = M('User')->where($where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
// 输出分页html
$this->assign('page',$Page->show());

3. Pagination page number jump function

In the ThinkPHP3.2 framework, the paging page number jumps The transfer function is implemented by adding input boxes and buttons in the paging style. The steps to add the page number jump function are as follows:

  1. Modify the paging style

Add an input box and a button in the custom paging style:

$Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% <div class="input-group input-group-sm" style="width: 150px;display:inline;"><input type="text" class="form-control" value="__PAGE__" onkeydown="javascript:if(event.keyCode==13){var page=(this.value|0);location.href=\&#39;__HREF__\&#39;.replace(\&#39;__PAGE__\&#39;,page);return false;}"><span class="input-group-btn"><button class="btn btn-default" onclick="javascript:var page=(this.previousSibling.value|0);location.href=\&#39;__HREF__\&#39;.replace(\&#39;__PAGE__\&#39;,page);return false;">GO!</button></span></div>');
  1. Use the paging page jump function

In the view file, you can directly use the customized paging style and implement the page jump function:

<div class="pagination">
    <?php echo $page;?>
</div>

Among them, in In the custom paging style, use "__PAGE__" and "__HREF__" to represent the current page number and jump link respectively. Enter the jump page number in the input box and click the button to jump to the page number.

Summary

In the ThinkPHP3.2 framework, the paging page number jump function can be implemented by adding input boxes and buttons in the paging style. Using custom paging styles can make the paging style more beautiful, and can also achieve more specific paging needs.

The above is the detailed content of Implementation and precautions of paging page number jump function in ThinkPHP3.2. For more information, please follow other related articles on the PHP Chinese website!

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