查出当前id所在的页面
字段id: 数值 1~27
分页时:每页显示 8数据
已知一个id,求这个id所在的页数
我的思路:
$arr=array();for(int i=0;i<maxpage;i++){$sql=(select id from table limit i*8,(i+1)*8); $arr[$i]+=$this->db->getAll($sql);}$arr={[0]=>(1~8)[1]=>(9~16)[2]=>(17~24)[3]=>(25~27)}让id 与数组$arr的value 比较 ,相同的话 获取出 key后+1就是它所在的页面
回复讨论(解决方案)
假定 id 连续,按 id 升序生成页面,则
$页号 = ceil($id / 8);
假定 id 连续,按 id 升序生成页面,则
$页号 = ceil($id / 8);
id不连续,用户有可能删除数据或被where过滤
有没有其它做法
不连续?那就需要知道 id 的分布情况
不连续?那就需要知道 id 的分布情况
id 的分布情况?
id就是不连续的
或者把数据库所有id都查出来,比较value,获取key,则 ceil((key+1)/8) 就是它所在的页数
如果数据多的多的话又很慢
已知一个id,求这个id在不连续id中正序的所在页数和位置(假设每页分X个)
eg
1: 已知id=5 id{1,2,5,9,10}, X=2 那么 已知id在第二页的第一个位置
2: 已知id=9 id{1,2,3,7,9,10} X=2 那么 已知id在第三页的第一个位置
这种只能是把所有的id取出来,
然后按 顺序编号,
然后得到指定id的编号,
$页号 = ceil( $编号 / 8);
有了 id 的分布,那就好计算了
//已知id=5 id{1,2,5,9,10}, X=2 那么 已知id在第二页的第一个位置$id = 5;$a = array(1,2,5,9,10);$x = 2;$p = array_search($id, $a);echo ceil($p / $x) + 1; //2//已知id=9 id{1,2,3,7,9,10} X=2 那么 已知id在第三页的第一个位置$id = 9;$a = array(1,2,5,9,10);$x = 2;$p = array_search($id, $a);echo ceil($p / $x) + 1; //3
$id = 9;$a = array(1,5,9,2,10);$x = 2;$p = array_search($id, $a);echo ceil($p / $x) + 1; //2
这种只能是把所有的id取出来,
然后按 顺序编号,
然后得到指定id的编号,
$页号 = ceil( $编号 / 8);
数据多的话,这种方法不好
或者 我求出最大页 第一页~最大页 获取第一个id 让已知id和第一个id进行比较 求出页数
这种只能是把所有的id取出来,
然后按 顺序编号,
然后得到指定id的编号,
$页号 = ceil( $编号 / 8);
数据多的话,这种方法不好
或者 我求出最大页 第一页~最大页 获取第一个id 让已知id和第一个id进行比较 求出页数
关键是你的需求,id是不连续的,你怎么知道每一页的第一个id是多少?
一样必须进行顺序编号,
不过简单一点,就是只 select 出0~指定id,就ok了
关于id这一块
1.一次性把所有id查询出来
2.从第一页到最大页
判断第一页第一个id 是否<已知id 是的话第二页第一个id 是否<已知id ........
这样查出来的数据好像少了些,但是db用的次数就多了
我还是用1把功能做出来先,如果有好的有方法告诉我,让我参考下
既然是要查询数据库,那么可以这样做
select id, ceil(@s/每页行数) as page, @s:=@s+1 from 表, (select @s:=1) t HAVING id=$id
有点问题,这样才对
select id, page from (select id, ceil(@s/每页行数) as page, @s:=@s+1 from 表, (select @s:=1) t ) t1 where id=$id
有点问题,这样才对
select id, page from (select id, ceil(@s/每页行数) as page, @s:=@s+1 from 表, (select @s:=1) t ) t1 where id=$id
@一般用来表示自己定义的一个变量
“:=”主要是用于传递外部参数为语句中变量赋值
@s:=@s+1 @s:=1 变量赋值两次?
(select @s:=1) t ) t1 t,t1没看懂
这种技巧性的写法要看悟性了
正常的写法是
set @s:=1;select id, ceil(@s/每页行数) as page, @s:=@s+1 from 表
但出于防止sql攻击的考虑,php 不允许同时执行两条及以上指令,所以使用了笛卡尔连接
(select @s:=1) t 只是为了给用户变量 @s 赋初值

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

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

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
