search
Homephp教程php手册php实现分页工具类分享

本文分享一个php实现的分页工具类,代码内有注释,大家参考使用吧

代码:

复制代码 代码如下:


    /**
    * 把中间的看成一个可以滑动的固定长度的尺子
    *
    * 把$this->_totalShowPages 当成一个可以滑动的固定长度尺子,
    * 然后$this->_totalPages就是一个给定长度的木块,尺子在这个
    * 木块上滑动。情况两种:
    * 1. 尺子长度大于木块长度,,那么就直接输出所有的页码;
    * 2. 尺子长度小于木块长度,那么就只用找到输出这个尺子长度页
    * 数的起始点——$start, $end;
    * @Access protected
    * @Return void
    * @Exception none
    */
    protected function _getShowPageNumber()
    {
    $pageHtml = '';
    //找到$start点
    if($this->_curPage - 2 > 1) {
    $start = $this->_curPage - 2;
    } else {
    $start = 1;
    }
    //找到$end 点
    $end = $start + $this->_totalShowPages;
    if($end >= $this->_totalPages) {
    $end = $this->_totalPages;
    $start = $end - $this->_totalShowPages; //保证页面显示的长度为$this->_totalShowPages
    }
    if($start != 1) {
    $pageHtml .= $this->_getPageHtml(1);
    $preMore = $this->_curPage - $this->_totalShowPages;
    if($preMore     $preMore = 1;
    }
    $pageHtml .= $this->_getMorePageHtml($preMore);
    }
    for($page = $start; $page     $pageHtml .= $this->_getPageHtml($page);
    }
    if($end != $this->_totalPages) {
    $pageHtml .= $this->_getMorePageHtml($end);
    }
    $pageHtml .= $this->_getNormalPageHtml($this->_totalPages);

    return $pageHtml;
    }
 

第一种老想法的代码实现:

复制代码 代码如下:


    /**
    * 一步步的来
    *
    * @desc
    *
    * @Access protected
    * @Return void
    * @Exception none
    */
    protected function _getShowPageNumberTwo()
    {
    if($this->_curPage _totalShowPages) {
    for($page = 1; $page _totalShowPages; $page ++) {
    $pageHtml .= $this->_getPageHtml($page);
    }
    $pageHtml .= $this->_getMorePageHtml($this->_totalShowPages);
    $pageHtml .= $this->_getNormalPageHtml($this->_totalPages);
    } else {
    $pageHtml .= $this->_getNormalPageHtml(1);
    if($this->_curPage == $this->_totalShowPages) {
    $pageHtml .= $this->_getMorePageHtml(1);
    } else {
    $pageHtml .= $this->_getMorePageHtml($this->_curPage - $this->_totalShowPages);
    }
    if($this->_curPage + $this->_totalShowPages >= $this->_totalPages) {
    for($page = $this->_totalPages - $this->_totalShowPages; $page _totalPages; $page ++) {
    $pageHtml .= $this->_getPageHtml($page);
    }
    } else {
    $start = $this->_curPage - 2;
    $end = $this->_curPage + $this->_totalShowPages - 2;
    for($page = $start; $page     $pageHtml .= $this->_getPageHtml($page);
    }
    $pageHtml .= $this->_getMorePageHtml($this->_curPage + $this->_totalShowPages - 2);
    $pageHtml .= $this->_getNormalPageHtml($this->_totalPages);
    }
    }

    return $pageHtml;
    }
   

Statement
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

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software