溫馨提示×

溫馨提示×

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

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

ThinkPHP實現(xiàn)分頁功能實例

發(fā)布時間:2021-08-11 19:31:21 來源:億速云 閱讀:125 作者:chen 欄目:開發(fā)技術(shù)

這篇文章主要講解了“ThinkPHP實現(xiàn)分頁功能實例”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“ThinkPHP實現(xiàn)分頁功能實例”吧!

本文實例講述了ThinkPHP分頁的實現(xiàn)方法,分享給大家供大家參考。具體方法如下:

在TP3.2框架手冊中有一個數(shù)據(jù)分頁,不過每次都要寫太多的代碼,還有中文設(shè)置等有些麻煩,做為程序開發(fā)者,有必要整理下:

實例運行效果如下圖所示:

ThinkPHP實現(xiàn)分頁功能實例

一、分頁方法:

復(fù)制代碼 代碼如下:

/**
 * TODO 基礎(chǔ)分頁的相同代碼封裝,使前臺的代碼更少
 * @param $m 模型,引用傳遞
 * @param $where 查詢條件
 * @param int $pagesize 每頁查詢條數(shù)
 * @return \Think\Page
 */
function getpage(&$m,$where,$pagesize=10){
    $m1=clone $m;//淺復(fù)制一個模型
    $count = $m->where($where)->count();//連慣操作后會對join等操作進行重置
    $m=$m1;//為保持在為定的連慣操作,淺復(fù)制一個模型
    $p=new Think\Page($count,$pagesize);
    $p->lastSuffix=false;
    $p->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>條記錄&nbsp;&nbsp;每頁<b>%LIST_ROW%</b>條&nbsp;&nbsp;第<b>%NOW_PAGE%</b>頁/共<b>%TOTAL_PAGE%</b>頁</li>');
    $p->setConfig('prev','上一頁');
    $p->setConfig('next','下一頁');
    $p->setConfig('last','末頁');
    $p->setConfig('first','首頁');
    $p->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

    $p->parameter=I('get.');

    $m->limit($p->firstRow,$p->listRows);

    return $p;
}

getpage方法可以放在TP框架的 Application/Common/Common/function.php,這個文檔可以專門放置一些通用的方法,在哪里都可以調(diào)用(如:Controller文件,View文件等)。

二、調(diào)用分頁方法

復(fù)制代碼 代碼如下:

$m=M('products');
$p=getpage($m,$where,10);
$list=$m->field(true)->where($where)->order('id desc')->select();
$this->list=$list;
$this->page=$p->show();
再是View代碼

<div class="pagination">
  {$page}
</div>
 

三、最后就是分頁的樣式了,這個有些亂,因后臺框架網(wǎng)上下載的,樣式還沒來的及整理,這個樣式也可以自己實現(xiàn),簡單的。

復(fù)制代碼 代碼如下:

.pagination ul {
    display: inline-block;
    margin-bottom: 0;
    margin-left: 0;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.pagination ul li {
  display: inline;
}

.pagination ul li.rows {
    line-height: 30px;
    padding-left: 5px;
}
.pagination ul li.rows b{color: #f00}

.pagination ul li a, .pagination ul li span {
    float: left;
    padding: 4px 12px;
    line-height: 20px;
    text-decoration: none;
    background-color: #fff;
    background: url('../images/bottom_bg.png') 0px 0px;
    border: 1px solid #d3dbde;
    /*border-left-width: 0;*/
    margin-left: 2px;
    color: #08c;
}
.pagination ul li a:hover{
    color: red;
    background: #0088cc;
}
.pagination ul li.first-child a, .pagination ul li.first-child span {
    border-left-width: 1px;
    -webkit-border-bottom-left-radius: 3px;
    border-bottom-left-radius: 3px;
    -webkit-border-top-left-radius: 3px;
    border-top-left-radius: 3px;
    -moz-border-radius-bottomleft: 3px;
    -moz-border-radius-topleft: 3px;
}
.pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover {
color: #999;
cursor: default;
background-color: transparent;
}
.pagination ul .active a, .pagination ul .active span {
color: #999;
cursor: default;
}
.pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span {
background-color: #f0c040;
}
.pagination ul li.last-child a, .pagination ul li.last-child span {
    -webkit-border-top-right-radius: 3px;
    border-top-right-radius: 3px;
    -webkit-border-bottom-right-radius: 3px;
    border-bottom-right-radius: 3px;
    -moz-border-radius-topright: 3px;
    -moz-border-radius-bottomright: 3px;
}

.pagination ul li.current a{color: #f00 ;font-weight: bold; background: #ddd}

感謝各位的閱讀,以上就是“ThinkPHP實現(xiàn)分頁功能實例”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對ThinkPHP實現(xiàn)分頁功能實例這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

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

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

AI