PHP anti-google paging effect code_PHP tutorial
function getPageRange($currentPage, $totalPages, $displaySize = 10) {
if ($totalPages <= 0 || $displaySize <= 0) {
return array();
} elseif ($displaySize > $totalPages) {
$startPage = 1;
$endPage = $totalPages;
} else {
if ($currentPage % $displaySize === 0) {
$startPage = $currentPage - $displaySize + 1;
} else {
while (($currentPage % $displaySize)) {
--$currentPage;
}
$startPage = $currentPage + 1;
}
if ($startPage <= 0) {
$startPage = 1;
}
$endPage = $startPage + $displaySize - 1;
if ($endPage > $totalPages) {
$endPage = $totalPages;
$startPage = $endPage - $displaySize + 1;
}
}
return range($startPage, $endPage);
}函数getPageRange接受三个参数,当前页$currentPage,总页数$totalPages,和翻页区间长度$displaySize,默认是10。根据这三个参数,函数getPageRange会生成一个适当的包含了$currentPage的翻页区间。首先,我们需要排除非法的参数值,对于总页数或区间长度小于零的情况,须加以检查。
然后,我们考察静态划分的思路。如前所述,给定翻页区间长度后,便可用总页数除以长度,得到区间个数。与此同时,我们可分析得知并不是所有区间都含有相同的页数,在极端情况下,还会出现总页数小于给定的翻页区间长度,那么划分或切割的结果将永远只有一个区间。幸运的是,这不会给我们的核心算法带来什么干扰,但我们仍须重视代码的健壮性。所以,我们先考虑极端情况,在运用算法解决核心问题之前,先迅速捕捉只有一页的区间。
接下来,我们便可以看看区间的固有属性了。每个动态切割的区间,都有一个起始页和一个尾页;由于区间彼此存在先后顺序,所以在经过静态划分后,我们始终会得到第一个(首)区间和最后一个(尾)区间,若首尾区间重合,则说明总页数小于给定的翻页区间长度。无论如何,算法需要解决的关键问题是如何找到区间的起始页和尾页,一旦确定这两个元素,便可以使用PHP内置的range函数生成区间内的全部页码。
算法利用当前页$currentPage和翻页区间长度$displaySize做比较,来判断当前页在区间内所处的位置,进而推导出区间起始页和尾页与当前页的偏移量。为了做到完美无缺,我们还要考虑边界溢出的问题,这也非常简单,只需判断起始页和尾页是否介于1和总页数之间即可。
至此,对于代码的分析已经完成。我们来看一下算法的时间效率,通俗地说,算法中的基本操作是求模,算法的执行时间取决于$currentPage与$displaySize的差值,差值越大,则求模次数越多、执行时间越长,呈线性结构。而实际的执行结果则是在瞬间完成的。
下面,我们来结合上面的Google搜索结果页,利用getPageRange函数生成一个翻页区间,输入所需参数:
print_r(implode(',', getPageRange(18, 27, 20)));得到的结果是:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20大家会发现这和Google搜索结果页显示的完全不一样。对,因为Google的翻页区间把当前页强制设置在中间位置上了!嗯,不要灰心,我们仍可以利用getPageRange函数得到与之相匹配的结果,只需把问题再分解一下:
print_r(implode(',', array_merge(getPageRange(17, 17, 10), getPageRange(27, 27, 10))));得到的结果是:
8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Google's AI Test Kitchen, which includes a suite of AI design tools for users to play with, has now opened up to users in well over 100 countries worldwide. This move marks the first time that many around the world will be able to use Imagen 3, Googl

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.
