溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

thinkphp如何增加每頁(yè)顯示條數(shù)

發(fā)布時(shí)間:2021-01-16 10:51:27 來源:億速云 閱讀:196 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下thinkphp如何增加每頁(yè)顯示條數(shù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

需求多加一個(gè)類似phpmyadmin一樣的每頁(yè)顯示條數(shù) 查了好久都沒找到

看到thinkphp 分頁(yè)類 是html拼接的 很low  但是方便了我修改 新增需求

在原生分頁(yè)類基礎(chǔ)上 新定義了一個(gè)num變量

show方法返回的時(shí)候 thinkphp拼接html的地方 新加了一段選擇條數(shù)的代碼

return "<ul class='am-pagination am-pagination-right'>{$page_str}</ul>
		<div class='am-dropdown  am-dropdown-up' data-am-dropdown>
			<button class='am-btn am-btn-primary am-dropdown-toggle' data-am-dropdown-toggle>顯示條數(shù) <span class='am-icon-caret-up'></span></button>
			<ul class='am-dropdown-content'>
				<li><a href='".$this->urlNum(10,1)."'>10</a></li>
				<li><a href='".$this->urlNum(30,1)."'>30</a></li>
				<li><a href='".$this->urlNum(50,1)."'>50</a></li>
			</ul>
		</div>";

然后新加的urlNum方法是這樣:

private function urlNum($num,$page){
    $str = str_replace(urlencode('[PAGE]'), $page, $this->url);
    return str_replace(urlencode('[NUM]'), $num,  $str);
}

開始的時(shí)候由于page這個(gè)變量 thinkphp會(huì)先變一個(gè)轉(zhuǎn)碼的 后面才替換

而且page=1的時(shí)候 url里是不顯示的 但是還有這個(gè)參數(shù)

導(dǎo)致num這個(gè)變量老是搞得url 很不穩(wěn)定 經(jīng)常疊加

后面只有做了一個(gè)小犧牲(選定每頁(yè)顯示條數(shù)的時(shí)候 url page即使為1 也會(huì)加上)

不過這并沒有什么影響

整個(gè)代碼分頁(yè)類 就是這樣:

<?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: 麥當(dāng)苗兒 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Think;

class Page{
    public $firstRow; // 起始行數(shù)
    public $listRows; // 列表每頁(yè)顯示行數(shù)
    public $parameter; // 分頁(yè)跳轉(zhuǎn)時(shí)要帶的參數(shù)
    public $totalRows; // 總行數(shù)
    public $totalPages; // 分頁(yè)總頁(yè)面數(shù)
    public $rollPage   = 11;// 分頁(yè)欄每頁(yè)顯示的頁(yè)數(shù)
	public $lastSuffix = true; // 最后一頁(yè)是否顯示總頁(yè)數(shù)

    private $p       = 'p'; //分頁(yè)參數(shù)名
    private $num       = 'num'; //分頁(yè)參數(shù)名
    private $url     = ''; //當(dāng)前鏈接URL
    private $nowPage = 1;
	// 分頁(yè)顯示定制
    private $config  = array(
        'header' => '<span class="rows">共 %TOTAL_ROW% 條記錄</span>',
        'prev'   => '&laquo;',
        'next'   => '&raquo;',
        'first'  => '1...',
        'last'   => '...%TOTAL_PAGE%',
        'theme'  => '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
    );

    /**
     * 架構(gòu)函數(shù)
     * @param array $totalRows  總的記錄數(shù)
     * @param array $listRows  每頁(yè)顯示記錄數(shù)
     * @param array $parameter  分頁(yè)跳轉(zhuǎn)的參數(shù)
     */
    public function __construct($totalRows, $listRows=20, $parameter = array()) {
        C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //設(shè)置分頁(yè)參數(shù)名稱
        /* 基礎(chǔ)設(shè)置 */
        $this->totalRows  = $totalRows; //設(shè)置總記錄數(shù)
        $this->listRows   = $listRows;  //設(shè)置每頁(yè)顯示行數(shù)
        $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);
    }

    /**
     * 定制分頁(yè)鏈接設(shè)置
     * @param string $name  設(shè)置名稱
     * @param string $value 設(shè)置值
     */
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name] = $value;
        }
    }

    /**
     * 生成鏈接URL
     * @param  integer $page 頁(yè)碼
     * @return string
     */
    private function url($page){
        // return str_replace(urlencode('[PAGE]'), $page, $this->url);
		$num = $_GET['num'] ? $_GET['num'] : '10';
        $str = str_replace(urlencode('[NUM]'), $num,   $this->url);
        return str_replace(urlencode('[PAGE]'), $page, $str);
    }
	
	private function urlNum($num,$page){
        $str = str_replace(urlencode('[PAGE]'), $page, $this->url);
        return str_replace(urlencode('[NUM]'), $num,  $str);
    }
    /**
     * 組裝分頁(yè)鏈接
     * @return string
     */
    public function show() {
        if(0 == $this->totalRows) return '';

        /* 生成URL */
		// echo $this->num;die;
        $this->parameter[$this->p] = '[PAGE]'; 
		// $num = empty($_GET['num']) ? '20' : '';
        $this->parameter[$this->num] = '[NUM]';
        $this->url = U(ACTION_NAME, $this->parameter);
        /* 計(jì)算分頁(yè)信息 */
        $this->totalPages = ceil($this->totalRows / $this->listRows); //總頁(yè)數(shù)
        if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
            $this->nowPage = $this->totalPages;
        }

        /* 計(jì)算分頁(yè)臨時(shí)變量 */
        $now_cool_page      = $this->rollPage/2;
		$now_cool_page_ceil = ceil($now_cool_page);
		$this->lastSuffix && $this->config['last'] = $this->totalPages;

        //上一頁(yè)
        $up_row  = $this->nowPage - 1;
        $up_page = $up_row > 0 ? '<li><a class="prev" href="' . $this->url($up_row) . '">' . $this->config['prev'] . '</a></li>' : '';

        //下一頁(yè)
        $down_row  = $this->nowPage + 1;
        $down_page = ($down_row <= $this->totalPages) ? '<li><a class="next" href="' . $this->url($down_row) . '">' . $this->config['next'] . '</a></li>' : '';

        //第一頁(yè)
        $the_first = '';
        if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
            $the_first = '<li><a class="first" href="' . $this->url(1) . '">' . $this->config['first'] . '</a></li>';
        }

        //最后一頁(yè)
        $the_end = '';
        if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
            $the_end = '<li><a class="end" href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a></li>';
        }

        //數(shù)字連接
        $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 .= '<li><a class="num" href="' . $this->url($page) . '">' . $page . '</a></li>';
                }else{
                    break;
                }
            }else{
                if($page > 0 && $this->totalPages != 1){
                    $link_page .= '<li class="am-active"><a href="#">' . $page . '</a></li>';
                }
            }
        }

        //替換分頁(yè)內(nèi)容
        $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 "<ul class='am-pagination am-pagination-right'>{$page_str}</ul>
					<div class='am-dropdown  am-dropdown-up' data-am-dropdown>
						<button class='am-btn am-btn-primary am-dropdown-toggle' data-am-dropdown-toggle>顯示條數(shù) <span class='am-icon-caret-up'></span></button>
						<ul class='am-dropdown-content'>
							<li><a href='".$this->urlNum(10,1)."'>10</a></li>
							<li><a href='".$this->urlNum(30,1)."'>30</a></li>
							<li><a href='".$this->urlNum(50,1)."'>50</a></li>
						</ul>
					</div>";
    }
}

效果如下:

thinkphp如何增加每頁(yè)顯示條數(shù)

以上是“thinkphp如何增加每頁(yè)顯示條數(shù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI