所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数:
每页多少条记录($PageSize)?
当前是第几页($CurrentPageID)?
现在只要再给我一个结果集,我就可以显示某段特定的结果出来。
至于其他的参数,比如:上一页($PreviousPageID)、下一页($NextPageID)、总页数($numPages)等等,都可以根据前边这几个东西得到。
以MySQL数据库为例,如果要从表内截取某段内容,sql语句可以用:select * from table limit offset, rows。看看下面一组sql语句,尝试一下发现其中的规率。
代码如下 | 复制代码 |
select * from table limit 0,10 // 前10条记录 select * from table limit 10,10 // 第11至20条记录 select * from table limit 20,10 // 第21至30条记录 |
……
这一组sql语句其实就是当$PageSize=10的时候取表内每一页数据的sql语句,我们可以总结出这样一个模板:
代码如下 | 复制代码 |
select * from table limit ($CurrentPageID - 1) * $PageSize, $PageSize |
拿这个模板代入对应的值和上边那一组sql语句对照一下看看是不是那么回事。搞定了最重要的如何获取数据的问题以后,剩下的就仅仅是传递参数,构造合适的sql语句然后使用php从数据库内获取数据并显示了。以下我将用具体代码加以说明。
例
代码如下 | 复制代码 | ||||
ShowData ShowData//连接数据库 $page = 1; $db = mysql_connect('127.0.0.1','root','toor'); mysql_select_db('test',$db); $pagesize = 3; //每页显示到数量 //计算一共有多少记录,用于计算页数 $rs = mysql_query("select count(*) from info",$db); $row = @mysql_fetch_array($rs); $numrows = $row[0]; //计算页数 //设置页数 //读取指定记录 //把数据用表格显示出来
} echo " 共".$pages."页(".$page."/".$pages.")"; ";for ($i = 1;$i { echo "[".$i."]"; } echo "[".$page."]"; for ($i = $page + 1;$i { echo "[".$i."]"; } echo " ?> |
总结,分页就根据用户点击连接,然后由于php获取page值再经过一系列算法得出当前多少页面,然后取多少要就显示出你要查看的记录了。
本文地址:
转载随意,但请附上文章地址:-)

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor