mysql中的分页主要是通过关键词limit来实现的, limit 可以给以给起始值,然后再给一个范围,就可以逐次地取出所有的数据。
举个简单的例子:
create table user(int 10 not null auto_increment,
name varchar(20),
sex tinyint(1),
reg_time datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
创建出user表,插入测试数据。
insert into user(`name`,`sex` `reg_time`) values('zhangsan','1','2012-02-09 02:00:00);
insert into user(`name`,`sex` `reg_time`) values('zaoren','1','2012-02-09 14:00:32);
insert into user(`name`,`sex` `reg_time`) values('lily','0','2013-08-10 10:06:00);
insert into user(`name`,`sex` `reg_time`) values('lizi','0','1956-00-56 10:10:50);
insert into user(`name`,`sex` `reg_time`) values('zhangsan2','1','1999-00-34 03:08:00);
insert into user(`name`,`sex` `reg_time`) values('zhangsan3','0','1998-00-24 00:23:00);
insert into user(`name`,`sex` `reg_time`) values('zhangsan4','1','2013-00-00 00:12:00);
insert into user(`name`,`sex` `reg_time`) values('qunhao','1','2014-00-00 00:00:00);
insert into user(`name`,`sex` `reg_time`) values('zhangsan4','1','2014-10-30 09:56:00);
insert into user(`name`,`sex` `reg_time`) values('zhangsan6','1','2013-06-00 05:01:00);
insert into user(`name`,`sex` `reg_time`) values('zhangsan8','1','2016-00-00 00:00:00);
insert into user(`name`,`sex` `reg_time`) values('hongyezhang','0','2015-00-00 00:00:00);
insert into user(`name`,`sex` `reg_time`) values('zhangsan','1','2008-08-080 00:00:00);
之后通过limit就可以分批取出用户信息:
select * from `user` order by `reg_time` limit 0,5;
select * from `user` order by `reg_time` limit 5,5;
select * from `user` order by `reg_time` limit 10,5;
通过改变limit 的初始值,就可以每次取出5条数据。
下面给出ecshop中的分页页码函数:
function get_pager($url, $param, $record_count, $page = 1, $size = 10)
{
$size = intval($size);
if ($size < 1)
{
$size = 10;
}
$page = intval($page);
if ($page < 1)
{
$page = 1;
}
$record_count = intval($record_count);
$page_count = $record_count > 0 ? intval(ceil($record_count / $size)) : 1;
if ($page > $page_count)
{
$page = $page_count;
}
/* 分页样式 */
$pager['styleid'] = isset($GLOBALS['_CFG']['page_style'])? intval($GLOBALS['_CFG']['page_style']) : 0;
$page_prev = ($page > 1) ? $page - 1 : 1;
$page_next = ($page < $page_count) ? $page + 1 : $page_count;
/* 将参数合成url字串 */
$param_url = '?';
foreach ($param AS $key => $value)
{
$param_url .= $key . '=' . $value . '&';
}
$pager['url'] = $url;
$pager['start'] = ($page -1) * $size;
$pager['page'] = $page;
$pager['size'] = $size;
$pager['record_count'] = $record_count;
$pager['page_count'] = $page_count;
if ($pager['styleid'] == 0)
{
$pager['page_first'] = $url . $param_url . 'page=1';
$pager['page_prev'] = $url . $param_url . 'page=' . $page_prev;
$pager['page_next'] = $url . $param_url . 'page=' . $page_next;
$pager['page_last'] = $url . $param_url . 'page=' . $page_count;
$pager['array'] = array();
for ($i = 1; $i <= $page_count; $i++)
{
$pager['array'][$i] = $i;
}
}
else
{
$_pagenum = 10; // 显示的页码
$_offset = 2; // 当前页偏移值
$_from = $_to = 0; // 开始页, 结束页
if($_pagenum > $page_count)
{
$_from = 1;
$_to = $page_count;
}
else
{
$_from = $page - $_offset;
$_to = $_from + $_pagenum - 1;
if($_from < 1)
{
$_to = $page + 1 - $_from;
$_from = 1;
if($_to - $_from < $_pagenum)
{
$_to = $_pagenum;
}
}
elseif($_to > $page_count)
{
$_from = $page_count - $_pagenum + 1;
$_to = $page_count;
}
}
$url_format = $url . $param_url . 'page=';
$pager['page_first'] = ($page - $_offset > 1 && $_pagenum < $page_count) ? $url_format . 1 : '';
$pager['page_prev'] = ($page > 1) ? $url_format . $page_prev : '';
$pager['page_next'] = ($page < $page_count) ? $url_format . $page_next : '';
$pager['page_last'] = ($_to < $page_count) ? $url_format . $page_count : '';
$pager['page_kbd'] = ($_pagenum < $page_count) ? true : false;
$pager['page_number'] = array();
for ($i=$_from;$i<=$_to;++$i)
{
$pager['page_number'][$i] = $url_format . $i;
}
}
$pager['search'] = $param;
return $pager;
}
从中我们可以看出他通过伪造url参数来传值,比如这里参数page当前页面就是通过url传参get过来的。
分页页码显示需要的几个变量:
当前页面变量,
数据的条数,
每页显示的条数,
根据上面得出的总页数,
这样就可以得出最大的页码数,就可以进行分页码显示了。
给出一个url?page=2,则当前是第二页,第二页页码没有连接,
前一页的页码自然是url?page=1(-1)
下一页的页码自然是url?page=3(+1)
当前页面的内容,就可以通过
select * from table order by id limit 2*size, size,
每次通过调整limit初始值就可以达到取出表中的所有数据。
分页的实现就是考虑2部分,一部分是数据的分页显示,一部分就是页码的显示,上面给出的函数就是ecshop 中页码的显示函数,数据的显示通过自己的调整来实现。