默默小谈PHP&MYSQL分页原理及实现
在看本文之前,请确保你已掌握了PHP的一些知识以及MYSQL的查询操作基础哦。
作为一个Web程序,经常要和不计其数的数据打交道,比如会员的数据,文章数据,假如只有几十个会员那很好办,在一页显示就可以了,可是假如你的网站是几千甚至几十万会员的话,如果都在一页打开的话无论对浏览器还是观看者都是一种折磨。
相信每个学习PHP的新手都会对分页这个东西感觉很头疼,不过有了默默的这一水帖,你肯定会拍拍脑袋说,嘿,原来分页竟然如此简单?的确,现在请深呼吸一口新鲜的空气,仔细的听默默给你一点一点的分解。
假设我们要处理1000条数据,要在每页中显示10条,这样的话就会分100页来显示,咱们先看一看在mysql里提取10条信息是如何操作的。
Select * from table limit 0,10
上面是一句很简单的mysql查询语句,它的作用是从一个名叫table的表里提取10条数据,并且把所有字段的值都获得。
关键的地方就在这段“limit 0,10”,它其中的0是以0为起始点,后面的10则是显示10条数据,那么我们要以10为起始点,显示到第20条数据该怎么写呢?
可能很多大大会心直口快的说“limit 10,20”嘛!啊哦,这样可就错误了哦,正确的写法是“limit 10,10”它后面的参数并非是结束点而是要提取的数目,记住哦。
懂得了如何提取10条数据,那么提取1000条也就是做100次这种查询呀,就是说要做如下的查询:
Limit 0,10 //第一页
Limit 10,10 //第二页
Limit 20,10 //第三页
Limit 30,10 //第四页
……
看出有什么规律了吗?没错,第一个参数每翻一页就增加10,可是第二个参数是不变的。
也就是说咱们设法根据页数来改变第一个参数的值,就可以进行分页显示数据了,怎么样,原理是不是很简单?
可是要怎么设法根据页数来改变第一个参数的值呢?首先,咱们要有一个页数的值,用url的GET方式获取。
比如index.php?page=18
相信大部分的大大对这个东西不陌生吧,这种url地址可是随处可见,其中的page参数的作用就是传入要显示的页数。
咱们通过一段代码来看一看究竟是如何实现的吧:
复制代码 代码如下:
/*
Author:默默
Date :2006-12-03
*/
$page=isset($_GET['page'])?intval($_GET['page']):1; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10; //每页显示10条数据
$db=mysql_connect("host","name","pass"); //创建数据库连接
$select=mysql_select_db("db",$db); //选择要操作的数据库
/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是
总数据数除以每页显示的条数,有余进一。
也就是说10/3=3.3333=4 有余数就要进一。
*/
$total=mysql_num_rows(mysql_query("select * from table")); //查询数据的总数
$pagenum=ceil($total/$num); //获得总页数
//假如传入的页数参数大于总页数,则显示错误信息
If($page>$pagenum || $page == 0){
Echo "Error : Can Not Found The page .";
Exit;
}
$offset=($page-1)*$num; //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。
$info=mysql_query("select * from table limit $offset,$num"); //获取相应页数所需要显示的数据
While($it=mysql_fetch_array($info)){
Echo $it['name']."
";
} //显示数据
For($i=1;$i
$show=($i!=$page)?"$i":"$i";
Echo $show." ";
}
/*显示分页信息,假如是当页则显示粗体的数字,其余的页数则为超连接,假如当前为第三页则显示如下
1 2 3 4 5 6
*/
?>
假如你仔细的读过上面的代码,把数据库连接和查询的表替换成你的,那么就能看见它的执行效果哦。
是不是很简单,只要动动脑筋,可以让它显示的更为个性化哦,给大家出一个小题,如何实现“首页 上一页 下一页 尾页”这种格式的分页呢?
OK,水帖灌完,收工。^_^
复制代码 代码如下:
/*
Author:默默
Date :2006-12-03
*/
$page=isset($_GET['page'])?intval($_GET['page']):1; //这句就是获取page=18中的page的值,假如不存在page,那么页数就是1。
$num=10; //每页显示10条数据
$db=mysql_connect("localhost","root","7529639"); //创建数据库连接
mysql_select_db("cr_download"); //选择要操作的数据库
/*
首先咱们要获取数据库中到底有多少数据,才能判断具体要分多少页,具体的公式就是
总数据库除以每页显示的条数,有余进一。
也就是说10/3=3.3333=4 有余数就要进一。
*/
$result=mysql_query("select * from cr_userinfo");
$total=mysql_num_rows($result); //查询所有的数据
$url='test.php';//获取本页URL
//页码计算
$pagenum=ceil($total/$num); //获得总页数,也是最后一页
$page=min($pagenum,$page);//获得首页
$prepg=$page-1;//上一页
$nextpg=($page==$pagenum ? 0 : $page+1);//下一页
$offset=($page-1)*$num; //获取limit的第一个参数的值,假如第一页则为(1-1)*10=0,第二页为(2-1)*10=10。
//开始分页导航条代码:
$pagenav="显示第 ".($total?($offset+1):0)."-".min($offset+10,$total)." 条记录,共 $total 条记录 ";
//如果只有一页则跳出函数:
if($pagenum
$pagenav.=" 首页 ";
if($prepg) $pagenav.=" 前页 "; else $pagenav.=" 前页 ";
if($nextpg) $pagenav.=" 后页 "; else $pagenav.=" 后页 ";
$pagenav.=" 尾页 ";
//下拉跳转列表,循环列出所有页码:
$pagenav.=" 到第 页,共 $pagenum 页";
//假如传入的页数参数大于总页数,则显示错误信息
If($page>$pagenum){
Echo "Error : Can Not Found The page ".$page;
Exit;
}
$info=mysql_query("select * from cr_userinfo limit $offset,$num"); //获取相应页数所需要显示的数据
While($it=mysql_fetch_array($info)){
Echo $it['username'];
echo "
";
} //显示数据
echo"
";
echo $pagenav;//输出分页导航
?>
顺便再深入下,在实际应用中,几乎涉及列表的就要用到分页,大家可以试试做一个分页通用函数,这样只要需要分页的地方就调用这个函数,呵呵~~

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

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

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

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

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
