關於thinkPHP3.2實作分頁自訂樣式的方法
這篇文章主要介紹了thinkPHP3.2實現分頁自訂樣式的方法,結合實例形式分析了thinkPHP3.2針對底層框架程式碼的修改與使用相關操作技巧,需要的朋友可以參考下
本文實例講述了thinkPHP3.2實作分頁自訂樣式的方法。分享給大家供大家參考,具體如下:
下面是一個Tp3.2的自訂分頁,這個方法也是在看過一個網友的部落格之後受到啟發這麼寫的。經過了一些修改,大家在看到程式碼之後也可以進行修改自訂樣式;
主要的樣式控制檔就是page.css,框架底層的分頁類別可以直接進行貼上複製使用;
1. 框架底層的page.class.php 路徑( Engine\Library\Think)
其實這個檔案不需要過多修改,也可以直接使用官方的就行;下面是我現在用的,稍作了修改;
<?php namespace Think; class Page{ public $firstRow; // 起始行数 public $listRows; // 列表每页显示行数 public $parameter; // 分页跳转时要带的参数 public $totalRows; // 总行数 public $totalPages; // 分页总页面数 public $rollPage = 11;// 分页栏每页显示的页数 public $lastSuffix = true; // 最后一页是否显示总页数 private $p = 'p'; //分页参数名 private $url = ''; //当前链接URL private $nowPage = 1; // 分页显示定制 private $config = array( 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>', 'prev' => '<<', 'next' => '>>', 'first' => '1...', 'last' => '...%TOTAL_PAGE%', 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%', ); /** * 架构函数 * @param array $totalRows 总的记录数 * @param array $listRows 每页显示记录数 * @param array $parameter 分页跳转的参数 */ public function __construct($totalRows, $listRows=20, $parameter = array()) { C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称 /* 基础设置 */ $this->totalRows = $totalRows; //设置总记录数 $this->listRows = $listRows; //设置每页显示行数 $this->parameter = empty($parameter) ? $_GET : $parameter; $this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]); $this->firstRow = $this->listRows * ($this->nowPage - 1); } /** * 定制分页链接设置 * @param string $name 设置名称 * @param string $value 设置值 */ public function setConfig($name,$value) { if(isset($this->config[$name])) { $this->config[$name] = $value; } } /** * 生成链接URL * @param integer $page 页码 * @return string */ private function url($page){ return str_replace(urlencode('[PAGE]'), $page, $this->url); } /** * 组装分页链接 * @return string */ public function show() { if(0 == $this->totalRows) return ''; /* 生成URL */ $this->parameter[$this->p] = '[PAGE]'; $this->url = U(ACTION_NAME, $this->parameter); /* 计算分页信息 */ $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数 if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) { $this->nowPage = $this->totalPages; } /* 计算分页零时变量 */ $now_cool_page = $this->rollPage/2; $now_cool_page_ceil = ceil($now_cool_page); $this->lastSuffix && $this->config['last'] = $this->totalPages; //上一页 $up_row = $this->nowPage - 1; $up_page = $up_row > 0 ? '<a class="prev" href="' . $this->url($up_row) . '" rel="external nofollow" >' . $this->config['prev'] . '</a>' : ''; //下一页 $down_row = $this->nowPage + 1; $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="' . $this->url($down_row) . '" rel="external nofollow" >' . $this->config['next'] . '</a>' : ''; //第一页 $the_first = ''; if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){ $the_first = '<a class="first" href="' . $this->url(1) . '" rel="external nofollow" >' . $this->config['first'] . '</a>'; } //最后一页 $the_end = ''; if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){ $the_end = '<a class="end" href="' . $this->url($this->totalPages) . '" rel="external nofollow" >' . $this->config['last'] . '</a>'; } //数字连接 $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 .= '<a class="num" href="' . $this->url($page) . '" rel="external nofollow" >' . $page . '</a>'; }else{ break; } }else{ if($page > 0 && $this->totalPages != 1){ $link_page .= '<span class="current">' . $page . '</span>'; } } } //替换分页内容 $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 "<p>{$page_str}</p>"; } }
#2. 控制器,隨便寫個demo。
public function index(){ $obj=M("news"); $count = $obj->where('status=1 and classID=74 ')->count();// 查询满足要求的总记录数 $limit = 10; $Page = new \Think\Page($count,$limit);// 实例化分页类 传入总记录数和每页显示的记录数(25) $show = $Page->show();// 分页显示输出 $list = $obj->where('status=1 and classID=74 ')->order('writetime desc')->limit($Page->firstRow.','.$Page->listRows)->select(); $firstlist = $obj->where('status=1 and classID=74 and Indexfirst=1')->order('writetime desc')->limit(4)->select(); $this->assign('firstlist',$firstlist); $this->assign('page',$show);// 赋值分页输出 $this->assign('list',$list); $this->display(); }
3. 接下來是View層,樣式控制。 page.css檔案
.b-page { background: #fff; box-shadow: 0px 1px 2px 0px #E2E2E2; } .page { width: 100%; background: #FFF; text-align: center; overflow: hidden; font-size:14px; margin-top:50px; } .page .first, .page .prev, .page .current, .page .num, .page .current, .page .next, .page .end { padding: 8px 16px; margin: 0px 5px; display: inline-block; color: #144970; border: 1px solid #F2F2F2; border-radius: 5px; } .page .first:hover, .page .prev:hover, .page .current:hover, .page .num:hover, .page .current:hover, .page .next:hover, .page .end:hover { text-decoration: none; background: #F8F5F5; } .page .current { background-color: #144970; color: #FFF; border-radius: 5px; } .page .current:hover { text-decoration: none; background: #144970; } .page .not-allowed { cursor: not-allowed; }
#以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
關於thinkPHP框架加入js事件分頁類別customPage.class.php的分析
####################################################################### #######
以上是關於thinkPHP3.2實作分頁自訂樣式的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

PHP開發:如何實現表格資料排序和分頁功能在進行Web開發中,處理大量資料是一項常見的任務。對於需要展示大量資料的表格,通常需要實現資料排序和分頁功能,以提供良好的使用者體驗和最佳化系統效能。本文將介紹如何使用PHP實作表格資料的排序和分頁功能,並給出具體的程式碼範例。排序功能實作在表格中實作排序功能,可以讓使用者根據不同的欄位進行升序或降序排序。以下是一個實作表格

CakePHP是一個強大的PHP框架,為開發人員提供了許多有用的工具和功能。其中之一是分頁,它可以幫助我們將大量資料分成幾頁,從而簡化瀏覽和操作。預設情況下,CakePHP提供了一些基本的分頁方法,但有時你可能需要建立一些自訂的分頁方法。這篇文章將向您展示如何在CakePHP中建立自訂分頁。步驟1:建立自訂分頁類別首先,我們需要建立一個自訂分頁類別。這個

隨著數據的不斷增長,表格顯示變得更加困難。大多數情況下,表格中的資料量過大,導致表格在載入時變得緩慢,而且使用者需要不斷地瀏覽頁面才能找到自己想要的資料。本文將介紹如何使用JavaScript實作表格資料的分頁顯示,讓使用者更容易找到自己想要的資料。一、動態建立表格為了讓分頁功能更可控,需要動態建立表格。在HTML頁面中,新增一個類似下面的table元素。

如何使用JavaScript實作表格分頁功能?隨著網路的發展,越來越多的網站都會使用表格來展示數據。在某些資料量較大的情況下,需要將資料進行分頁展示,以提升使用者體驗。本文將介紹如何使用JavaScript實作表格分頁功能,並提供具體的程式碼範例。一、HTML結構首先,我們需要準備一個HTML結構來承載表格和分頁按鈕。我們可以使用<tab

Vue元件實戰:分頁元件開發介紹在網路應用程式中,分頁功能是不可或缺的一個元件。一個好的分頁元件應該展示簡潔明了,功能豐富,而且易於整合和使用。在本文中,我們將介紹如何使用Vue.js框架來開發一個高度可自訂化的分頁元件。我們將透過程式碼範例來詳細說明如何使用Vue元件開發。技術堆疊Vue.js2.xJavaScript(ES6)HTML5和CSS3開發環

MyBatis是一個優秀的持久層框架,它支援基於XML和註解的方式操作資料庫,簡單易用,同時也提供了豐富的插件機制。其中,分頁插件是使用頻率較高的插件之一。本文將深入探討MyBatis分頁外掛的原理,並結合具體的程式碼範例進行說明。一、分頁外掛原理MyBatis本身並沒有提供原生的分頁功能,但可以藉助外掛程式來實現分頁查詢。分頁插件的原理主要是透過攔截MyBatis

如何利用Layui開發一個具有分頁功能的資料展示頁面Layui是一個輕量級的前端UI框架,提供了簡潔美觀的介面元件和豐富的互動體驗。在開發中,我們經常會遇到需要展示大量資料並進行分頁的情況。以下是利用Layui開發的具有分頁功能的資料展示頁面的範例。首先,我們需要引入Layui的相關文件和依賴。在html頁面的<head>標籤中加入以下代

Vue是一種流行的JavaScript框架,用於建立使用者介面。在Vue技術開發中,實現分頁功能是常見的需求。本文將介紹如何使用Vue來實現分頁功能,並提供具體程式碼範例。在開始之前,我們需要提前準備一些基本知識。首先,我們需要了解Vue的基本概念和文法。其次,我們需要知道如何使用Vue元件來建立我們的應用程式。在開始之前,我們需要在Vue專案中安裝一個分頁插件,
