ThinkPHP 分頁實現(xiàn),thinkphp分頁實現(xiàn)
Jun 13, 2016 am 09:24 AMThinkPHP 分頁實現(xiàn),thinkphp分頁實現(xiàn)
TP3.2框架手冊,有一個數(shù)據(jù)分頁,不過每次都要寫太多的代碼,還有中文設(shè)置等有些麻煩,做為程序開發(fā)者,有必要整理下:
O、先看效果圖
一、分頁方法
<span>/*</span><span>* * TODO 基礎(chǔ)分頁的相同代碼封裝,使前臺的代碼更少 * @param $m 模型,引用傳遞 * @param $where 查詢條件 * @param int $pagesize 每頁查詢條數(shù) * @return \Think\Page </span><span>*/</span> <span>function</span> getpage(&<span>$m</span>,<span>$where</span>,<span>$pagesize</span>=10<span>){ </span><span>$m1</span>=<span>clone</span> <span>$m</span>;<span>//</span><span>淺復(fù)制一個模型</span> <span>$count</span> = <span>$m</span>->where(<span>$where</span>)-><span>count</span>();<span>//</span><span>連慣操作后會對join等操作進行重置</span> <span>$m</span>=<span>$m1</span>;<span>//</span><span>為保持在為定的連慣操作,淺復(fù)制一個模型</span> <span>$p</span>=<span>new</span> Think\Page(<span>$count</span>,<span>$pagesize</span><span>); </span><span>$p</span>->lastSuffix=<span>false</span><span>; </span><span>$p</span>->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>條記錄 每頁<b>%LIST_ROW%</b>條 第<b>%NOW_PAGE%</b>頁/共<b>%TOTAL_PAGE%</b>頁</li>'<span>); </span><span>$p</span>->setConfig('prev','上一頁'<span>); </span><span>$p</span>->setConfig('next','下一頁'<span>); </span><span>$p</span>->setConfig('last','末頁'<span>); </span><span>$p</span>->setConfig('first','首頁'<span>); </span><span>$p</span>->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'<span>); </span><span>$p</span>->parameter=I('get.'<span>); </span><span>$m</span>->limit(<span>$p</span>->firstRow,<span>$p</span>-><span>listRows); </span><span>return</span> <span>$p</span><span>; }</span>
getpage方法可以放在TP框架的 Application/Common/Common/function.php,這個文檔可以專門放置一些通用的方法,在哪里都可以調(diào)用(如:Controller文件,View文件等)。
二、調(diào)用分頁方法
<span>$m</span>=M('products'<span>); </span><span>$p</span>=getpage(<span>$m</span>,<span>$where</span>,10<span>); </span><span>$list</span>=<span>$m</span>->field(<span>true</span>)->where(<span>$where</span>)->order('id desc')-><span>select(); </span><span>$this</span>-><span>list</span>=<span>$list</span><span>; </span><span>$this</span>->page=<span>$p</span>->show();
再是View代碼
<div class="pagination"><span> {$page} </div></span>
三、最后就是分頁的樣式了,這個有些亂,因后臺框架網(wǎng)上下載的,樣式還沒來的及整理,這個樣式也可以自己實現(xiàn),簡單的。
<span>.pagination ul </span>{<span> display</span>:<span> inline-block</span>;<span> margin-bottom</span>:<span> 0</span>;<span> margin-left</span>:<span> 0</span>;<span> -webkit-border-radius</span>:<span> 3px</span>;<span> -moz-border-radius</span>:<span> 3px</span>;<span> border-radius</span>:<span> 3px</span>;<span> -webkit-box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>;<span> -moz-box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>;<span> box-shadow</span>:<span> 0 1px 2px rgba(0,0,0,0.05)</span>; }<span> .pagination ul li </span>{<span> display</span>:<span> inline</span>; }<span> .pagination ul li.rows </span>{<span> line-height</span>:<span> 30px</span>;<span> padding-left</span>:<span> 5px</span>; }<span> .pagination ul li.rows b</span>{<span>color</span>:<span> #f00</span>}<span> .pagination ul li a, .pagination ul li span </span>{<span> float</span>:<span> left</span>;<span> padding</span>:<span> 4px 12px</span>;<span> line-height</span>:<span> 20px</span>;<span> text-decoration</span>:<span> none</span>;<span> background-color</span>:<span> #fff</span>;<span> background</span>:<span> url('../images/bottom_bg.png') 0px 0px</span>;<span> border</span>:<span> 1px solid #d3dbde</span>; <span>/*</span><span>border-left-width: 0;</span><span>*/</span><span> margin-left</span>:<span> 2px</span>;<span> color</span>:<span> #08c</span>; }<span> .pagination ul li a:hover</span>{<span> color</span>:<span> red</span>;<span> background</span>:<span> #0088cc</span>; }<span> .pagination ul li.first-child a, .pagination ul li.first-child span </span>{<span> border-left-width</span>:<span> 1px</span>;<span> -webkit-border-bottom-left-radius</span>:<span> 3px</span>;<span> border-bottom-left-radius</span>:<span> 3px</span>;<span> -webkit-border-top-left-radius</span>:<span> 3px</span>;<span> border-top-left-radius</span>:<span> 3px</span>;<span> -moz-border-radius-bottomleft</span>:<span> 3px</span>;<span> -moz-border-radius-topleft</span>:<span> 3px</span>; }<span> .pagination ul .disabled span, .pagination ul .disabled a, .pagination ul .disabled a:hover </span>{<span> color</span>:<span> #999</span>;<span> cursor</span>:<span> default</span>;<span> background-color</span>:<span> transparent</span>; }<span> .pagination ul .active a, .pagination ul .active span </span>{<span> color</span>:<span> #999</span>;<span> cursor</span>:<span> default</span>; }<span> .pagination ul li a:hover, .pagination ul .active a, .pagination ul .active span </span>{<span> background-color</span>:<span> #f0c040</span>; }<span> .pagination ul li.last-child a, .pagination ul li.last-child span </span>{<span> -webkit-border-top-right-radius</span>:<span> 3px</span>;<span> border-top-right-radius</span>:<span> 3px</span>;<span> -webkit-border-bottom-right-radius</span>:<span> 3px</span>;<span> border-bottom-right-radius</span>:<span> 3px</span>;<span> -moz-border-radius-topright</span>:<span> 3px</span>;<span> -moz-border-radius-bottomright</span>:<span> 3px</span>; }<span> .pagination ul li.current a</span>{<span>color</span>:<span> #f00 </span>;<span>font-weight</span>:<span> bold</span>;<span> background</span>:<span> #ddd</span>}
?
TP的分頁其實是很簡單的:
首先你需要導(dǎo)入分頁類。
代碼如下:
$User = M('User'); // 實例化User對象
import('ORG.Util.Page');// 導(dǎo)入分頁類
$count = $User->where('status=1')->count();// 查詢滿足要求的總記錄數(shù)
$Page = new Page($count,25);// 實例化分頁類 傳入總記錄數(shù)和每頁顯示的記錄數(shù)
$show = $Page->show();// 分頁顯示輸出
// 進行分頁數(shù)據(jù)查詢 注意limit方法的參數(shù)要使用Page類的屬性
$list = $User->where('status=1')->order('create_time')->limit($Page->firstRow.','.$Page->listRows)->select();
$this->assign('list',$list);// 賦值數(shù)據(jù)集
$this->assign('page',$show);// 賦值分頁輸出
$this->display(); // 輸出模板
?
文章分頁,我記得應(yīng)該有個thinkphpcms的 你找找那個 安裝一遍看看那個怎么寫的。。你行的 - -
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.
