Home php教程 php手册 page 分页类

page 分页类

Jun 07, 2016 am 11:45 AM

我也是想做个自己的分页类,所以在案例里面加入了注释,如果不地方不对,请告诉我
<?php <br /> // +----------------------------------------------------------------------<br> // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]<br> // +----------------------------------------------------------------------<br> // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.<br> // +----------------------------------------------------------------------<br> // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )<br> // +----------------------------------------------------------------------<br> // | Author: liu21st <liu21st><br> // |         lanfengye <zibin_5257><br> // +----------------------------------------------------------------------<br> <br> class Page {<br>     <br>     // 分页栏每页显示的页数<br>     public $rollPage = 5;<br>     // 页数跳转时要带的参数<br>     public $parameter  ;<br>     // 默认列表每页显示行数<br>     public $listRows = 20;<br>     // 起始行数<br>     public $firstRow    ;<br>     // 分页总页面数<br>     protected $totalPages  ;<br>     // 总行数<br>     protected $totalRows  ;<br>     // 当前页数<br>     protected $nowPage    ;<br>     // 分页的栏的总页数<br>     protected $coolPages   ;<br>     // 分页显示定制<br>     protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>'%upPage% %first%  %prePage%  %linkPage%  %nextPage% %downPage% %end%');<br>     // protected $config  =    array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');<br>     // 默认分页变量名<br>     protected $varPage;<br> <br>     /**<br>      * 架构函数<br>      * @access public<br>      * @param array $totalRows  总的记录数<br>      * @param array $listRows  每页显示记录数<br>      * @param array $parameter  分页跳转的参数<br>      */<br>     public function __construct($totalRows,$listRows='',$parameter='') {<br>         $this->totalRows    =   $totalRows; // 构造函数参数 1,总页数<br>         $this->parameter    =   $parameter; // 构造函数参数 3,URL 附加参数<br>         $this->varPage      =   C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ; // 获取分页变量名,如果未定义则定义默认分页变量名<br>         /**<br>          * intval() 将变量转成整数类型<br>          */<br>         if(!empty($listRows)) { // 构造函数参数 2,获取每页显示的条数,如果每页显示的条数不为空则<br>             $this->listRows =   intval($listRows); // 转换为整型并赋值给每页显示的条数<br>         }<br>         /**<br>          * ceil() 函数向上舍入为最接近的整数(1.1=2)<br>          */<br>         $this->totalPages   =   ceil($this->totalRows/$this->listRows); // 获取总页数,记录集的总数除以每页显示的条数等于总页数<br>         // 假设有 40 条数据,每页显示 5 条,就是有 8 页,每个页面显示 2 个导航栏,就是有4栏<br>         $this->coolPages    =   ceil($this->totalPages/$this->rollPage); // 获取总栏数, 总栏数除以每页显示的栏数等于总栏数<br>         /**<br>          * empty() 如果参数是非空或非零的值,则返回 FALSE,否则返回 TRUE<br>          */<br>         $this->nowPage      =   !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1; // 获取当前页数,如果 URL 当前页数参数不为空则转换整型并赋值给当前页数,否则赋值为 1<br>         if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) { // 如果总页数不为空并且当前页数大于总页数则<br>             $this->nowPage  =   $this->totalPages; // 赋值当前页数为总页数<br>         }<br>         // 假设当前页数为 2,每页显示 5 条数据,当前页面就是从第 (5*(2-1)=5) 条记录开始读取数据,<br>         // 根据 limit 函数定义,索引从零开始,也就是实际的值是记录集的第六条数据<br>         $this->firstRow     =   $this->listRows*($this->nowPage-1); // 获取起始页,起始行数等于每页显示的条数乘以当前页面减 1<br>     }<br>     /**<br>      * 自定义导航显示<br>      * @access public<br>      * @param String $name 待替换的参数名称<br>      * @param String $value 替换的参数值<br>      * isset() 返回  bool 值<br>      * 若变量不存在则返回 FALSE <br>      * 若变量存在且其值为NULL,也返回 FALSE <br>      * 若变量存在且值不为NULL,则返回 TURE <br>      */    <br>     public function setConfig($name,$value) {<br>         if(isset($this->config[$name])) {<br>             $this->config[$name]    =   $value;<br>         }<br>     }<br> <br>     /**<br>      * 分页显示输出<br>      * @access public<br>      * @author lanfengye <zibin_5257><br>      */<br>     public function show() {<br>         if(0 == $this->totalRows) return '';<br>         $p              =   $this->varPage; // 默认分页变量名<br>         // 假设 40 条数据,每页显示 5 条,导航每页显示 4 栏,当前为第 3 页,也就是 ceil(3/4)=1<br>         $nowCoolPage    =   ceil($this->nowPage/$this->rollPage); // 当前分页栏<br>         <br>         //获取控制器名和方法名,并判断是否url不区分大小写<br>         $url_case       =   C('URL_CASE_INSENSITIVE');<br>         $module_name    =   $url_case?  parse_name(MODULE_NAME) :   MODULE_NAME;<br>         $action_name    =   $url_case?  parse_name(ACTION_NAME) :   ACTION_NAME;<br>         <br>         //替换附加参数中的分隔符<br>         $parameter      =   str_replace(array('&','='), C('URL_PATHINFO_DEPR'), $this->parameter);<br> <br>         //增加附加参数<br>         $url            =   rtrim(.'/'.$module_name.C('URL_PATHINFO_DEPR').$action_name.C('URL_PATHINFO_DEPR').$parameter,C('URL_PATHINFO_DEPR'));<br>         <br>         //上翻页字符串<br>         $upRow          =   $this->nowPage-1; // 上一页等于当前页减 1<br>         // 假设有 40 条记录,每页显示 5 条,当前页数为 1 的时候,上一页就会出现等于 0 的情况。<br>         if ($upRow>0){ // 如果上一页大于零,输出上一页的链接,            <br>             $upPage     =   "<a>".$this->config['prev']."</a>";<br>         }else{ // 如果上一页小于零,说明当前已经是第一页,不需要上一页链接输出<br>             $upPage     =   '';<br>         }<br>         <br>         // 下翻页字符串<br>         $downRow        =   $this->nowPage+1; // 下一页等于当前页加 1<br>         // 假设有 40 条记录,每页显示 5 条,当前页数为 7 的时候,下一页就会出现等于总页数的情况。<br>         if ($downRow totalPages){ // 如果下一页小于等于总页数,输出下一页链接<br>             $downPage   =   "<a>".$this->config['next']."</a>";<br>         }else{ // 如果下一页小于零,说明当前已经是最后一页,不需要下一页链接输出<br>             $downPage   =   '';<br>         }<br>         <br>         //          if($nowCoolPage == 1){ // 如果当前分页栏数为 1 时,当前在第一栏,<br>             $theFirst   =   '';<br>             $prePage    =   '';<br>         }else{ // 否则不在第一栏,输出上一栏链接<br>             $preRow     =   $this->nowPage-$this->rollPage; // 通过当前页数减去每页显示的条页,获取上一页的链接并,并输出<br>             $prePage    =   "<a>上".$this->rollPage."页</a>";<br>             // 输出第一页的链接<br>             $theFirst   =   "<a>".$this->config['first']."</a>";<br>         }<br>         <br>         // > >><br>         if($nowCoolPage == $this->coolPages){ // 如果当前栏等于最后一栏,当前栏为最后一栏<br>             $nextPage   =   '';<br>             $theEnd     =   '';<br>         }else{ // 否则不在最后一栏,输出最后一栏链接<br>             $nextRow    =   $this->nowPage+$this->rollPage; // 通过当前页数加上每页显示的条页,获取下一页的链接并,并输出<br>             $theEndRow  =   $this->totalPages;<br>             $nextPage   =   "<a>下".$this->rollPage."页</a>";<br>             // 输出最后一条链接<br>             $theEnd     =   "<a>".$this->config['last']."</a>";<br>         }<br>         <br>         // 1 2 3 4 5<br>         $linkPage = "";<br>         for($i=1;$irollPage;$i++){<br>             // 在96行定义,假设每页显示 5 条数据,<br>             // 当栏数为 1 时,公式等于{(1-1)*5+i},page 值等于 1、2、3、4、5<br>             // 当栏数为 2 时,公式等于{(2-1)*5+i},page 值等于 6、7、8、9、10            <br>             $page       =   ($nowCoolPage-1)*$this->rollPage+$i;<br>             if($page!=$this->nowPage){ // 当 page 值不等于当前页数的时<br>                 if($pagetotalPages){ // 当 page 值小于等于总页数时,输出链接<br>                     $linkPage .= " <a> ".$page." </a>";<br>                 }else{ // 否则返回<br>                     break;<br>                 }<br>             }else{ // 否则并且  page 值不等于 1 输出当前页面数(无链接)<br>                 if($this->totalPages != 1){ // ?????有没有这个判断貌似一样?????<br>                     $linkPage .= " <span>".$page."</span>";<br>                 }<br>             }<br>         }<br>         <br>         $pageStr     =     str_replace(<br>             array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),<br>             array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);<br>         return $pageStr;<br>     }<br> <br> }</zibin_5257></zibin_5257></liu21st>

AD:真正免费,域名+虚机+企业邮箱=0元

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn about introductory code examples for Python programming Learn about introductory code examples for Python programming Jan 04, 2024 am 10:50 AM

Learn about Python programming with introductory code examples Python is an easy-to-learn, yet powerful programming language. For beginners, it is very important to understand the introductory code examples of Python programming. This article will provide you with some concrete code examples to help you get started quickly. Print HelloWorldprint("HelloWorld") This is the simplest code example in Python. The print() function is used to output the specified content

PHP variables in action: 10 real-life examples of use PHP variables in action: 10 real-life examples of use Feb 19, 2024 pm 03:00 PM

PHP variables store values ​​during program runtime and are crucial for building dynamic and interactive WEB applications. This article takes an in-depth look at PHP variables and shows them in action with 10 real-life examples. 1. Store user input $username=$_POST["username"];$passWord=$_POST["password"]; This example extracts the username and password from the form submission and stores them in variables for further processing. 2. Set the configuration value $database_host="localhost";$database_username="username";$database_pa

From beginner to proficient: Code implementation of commonly used data structures in Go language From beginner to proficient: Code implementation of commonly used data structures in Go language Mar 04, 2024 pm 03:09 PM

Title: From Beginner to Mastery: Code Implementation of Commonly Used Data Structures in Go Language Data structures play a vital role in programming and are the basis of programming. In the Go language, there are many commonly used data structures, and mastering the implementation of these data structures is crucial to becoming a good programmer. This article will introduce the commonly used data structures in the Go language and give corresponding code examples to help readers from getting started to becoming proficient in these data structures. 1. Array Array is a basic data structure, a group of the same type

Java implements simple bubble sort code Java implements simple bubble sort code Jan 30, 2024 am 09:34 AM

The simplest code example of Java bubble sort Bubble sort is a common sorting algorithm. Its basic idea is to gradually adjust the sequence to be sorted into an ordered sequence through the comparison and exchange of adjacent elements. Here is a simple Java code example that demonstrates how to implement bubble sort: publicclassBubbleSort{publicstaticvoidbubbleSort(int[]arr){int

Go language programming examples: code examples in web development Go language programming examples: code examples in web development Mar 04, 2024 pm 04:54 PM

"Go Language Programming Examples: Code Examples in Web Development" With the rapid development of the Internet, Web development has become an indispensable part of various industries. As a programming language with powerful functions and superior performance, Go language is increasingly favored by developers in web development. This article will introduce how to use Go language for Web development through specific code examples, so that readers can better understand and use Go language to build their own Web applications. 1. Simple HTTP Server First, let’s start with a

Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Huawei Cloud Edge Computing Interconnection Guide: Java code examples to quickly implement interfaces Jul 05, 2023 pm 09:57 PM

Huawei Cloud Edge Computing Interconnection Guide: Java Code Samples to Quickly Implement Interfaces With the rapid development of IoT technology and the rise of edge computing, more and more enterprises are beginning to pay attention to the application of edge computing. Huawei Cloud provides edge computing services, providing enterprises with highly reliable computing resources and a convenient development environment, making edge computing applications easier to implement. This article will introduce how to quickly implement the Huawei Cloud edge computing interface through Java code. First, we need to prepare the development environment. Make sure you have the Java Development Kit installed (

How to use PHP to write inventory management function code in the inventory management system How to use PHP to write inventory management function code in the inventory management system Aug 06, 2023 pm 04:49 PM

How to use PHP to write the inventory management function code in the inventory management system. Inventory management is an indispensable part of many enterprises. For companies with multiple warehouses, the inventory management function is particularly important. By properly managing and tracking inventory, companies can allocate inventory between different warehouses, optimize operating costs, and improve collaboration efficiency. This article will introduce how to use PHP to write code for inventory warehouse management functions, and provide you with relevant code examples. 1. Establish the database before starting to write the code for the inventory warehouse management function.

Guidance and Examples: Learn to implement the selection sort algorithm in Java Guidance and Examples: Learn to implement the selection sort algorithm in Java Feb 18, 2024 am 10:52 AM

Java Selection Sorting Method Code Writing Guide and Examples Selection sorting is a simple and intuitive sorting algorithm. The idea is to select the smallest (or largest) element from the unsorted elements each time and exchange it until all elements are sorted. This article will provide a code writing guide for selection sorting, and attach specific Java sample code. Algorithm Principle The basic principle of selection sort is to divide the array to be sorted into two parts, sorted and unsorted. Each time, the smallest (or largest) element is selected from the unsorted part and placed at the end of the sorted part. Repeat the above

See all articles