很多程序朋友在写分页是特别是mysql有了limit n,m;这样的写法,分页从此简单了,但方不知道这种分页几万数据没有问题,但在百万千万级时就无法使用了,今天我们来介绍这两种分页的优化方法。
php写功能时,只要用的还是mysql,基本都是两步走
1、取得总数,算页数。sql语句自然是
代码如下 复制代码
select count(*) from tablename;
2、根据指定的页码号,取得相应的数据。对应的sql语句,在网上随便查,都是一样的:
select f1,f2 from table limit offset,length
实例分页类
代码如下 复制代码
/*********************************************
类名: pagesupport
功能:分页显示mysql中的数据
***********************************************/
class pagesupport{
//属性
var $sql; //所要显示数据的sql查询语句
var $page_size; //每页显示最多行数
var $start_index; //所要显示记录的首行序号
var $total_records; //记录总数
var $current_records; //本页读取的记录数
var $result; //读出的结果
var $total_pages; //总页数
var $current_page; //当前页数
var $display_count = 30; //显示的前几页和后几页数
var $arr_page_query; //数组,包含分页显示需要传递的参数
var $first;
var $prev;
var $next;
var $last;
//方法
/*********************************************
构造函数:__construct()
输入参数:
$ppage_size:每页显示最多行数
***********************************************/
function pagesupport($ppage_size)
{
$this->page_size=$ppage_size;
$this->start_index=0;
}
/*********************************************
构造函数:__destruct()
输入参数:
***********************************************/
function __destruct()
{
}
/*********************************************
get函数:__get()
***********************************************/
function __get($property_name)
{
if(isset($this->$property_name))
{
return($this->$property_name);
}
else
{
return(null);
}
}
/*********************************************
set函数:__set()
***********************************************/
function __set($property_name, $value)
{
$this->$property_name = $value;
}
/*********************************************
函数名:read_data
功能: 根据sql查询语句从表中读取相应的记录
返回值:属性二维数组result[记录号][字段名]
***********************************************/
function read_data()
{
$ql=$this->sql;
//查询数据,数据库链接等信息应在类调用的外部实现
$result=_query($psql) or die(mysql_error());
$this->total_records=mysql_num_rows($result);
//利用limit关键字获取本页所要显示的记录
if($this->total_records>0)
{
$this->start_index = ($this->current_page-1)*$this->page_size;
$psql=$psql. limit .$this->start_index. , .$this->page_size;
$result=mysql_query($psql) or die(mysql_error());
$this->current_records=mysql_num_rows($result);
//将查询结果放在result数组中
$i=0;
while($row=mysql_fetch_array($result))
{
$this->result[$i]=$row;
$i++;
}
}
//获取总页数、当前页信息
$this->total_pages=ceil($this->total_records/$this->page_size);
$this->first=1;
$this->prev=$this->current_page-1;
$this->next=$this->current_page+1;
$this->last=$this->total_pages;
}
/*********************************************
函数名:standard_navigate()
功能: 显示首页、下页、上页、未页
***********************************************/
function standard_navigate()
{
echo
;
echo ;echo 第.$this->current_page.页/共.$this->total_pages.页;
echo ;
echo 跳到current_page.'>页;
echo ;
//生成导航链接
if ($this->current_page > 1) {
echo first.>首页|;
echo prev.>上一页|;
}
if( $this->current_page total_pages) {
echo next.>下一页|;
echo last.>末页;
}
echo
;
echo
;}
/*********************************************
函数名:full_navigate()
功能: 显示首页、下页、上页、未页
生成导航链接 如1 2 3 ... 10 11
***********************************************/
function full_navigate()
{
echo
;
echo ;echo 第.$this->current_page.页/共.$this->total_pages.页;
echo ;
echo 跳到current_page.'>页;
echo ;
//生成导航链接 如1 2 3 ... 10 11
$front_start = 1;
if($this->current_page > $this->display_count){
$front_start = $this->current_page - $this->display_count;
}
for($i=$front_start;$icurrent_page;$i++){
echo [.$i .] ;
}
echo [.$this->current_page.];
$displaycount = $this->display_count;
if($this->total_pages > $displaycount&&($this->current_page+$displaycount)total_pages){
$displaycount = $this->current_page+$displaycount;
}else{
$displaycount = $this->total_pages;
}
for($i=$this->current_page+1;$iecho [.$i .] ;
}
//生成导航链接
if ($this->current_page > 1) {
echo first.>首页|;
echo prev.>上一页|;
}
if( $this->current_page total_pages) {
echo next.>下一页|;
echo last.>末页;
}
echo
;
echo
;}
}
?>
调用:
assign('title', smarty新闻分页测试);
__set(current_page,$current_page);
} else {
$pagesupport->__set(current_page,1);
}
?>
$pagesupport->__set(sql, * from news );
$pagesupport->read_data();//读数据
if ($pagesupport->current_records > 0) //如果数据不为空,则组装数据
{
for ($i=0; $icurrent_records; $i++)
{
$title = $pagesupport->result[$i][title];
$id = $pagesupport->result[$i][id];
$news_arr[$i] = array('news' => array('id' => $id,'title' => $title));
}
}
//关闭数据库
mysql_close($db);
$pageinfo_arr = array(
'total_records' => $pagesupport->total_records,
'current_page' => $pagesupport->current_page,
'total_pages' => $pagesupport->total_pages,
'first' => $pagesupport->first,
'prev' => $pagesupport->prev,
'next' => $pagesupport->next,
'last' => $pagesupport->last
);
$smarty->assign('results', $news_arr);
$smarty->assign('pagesupport', $pageinfo_arr);
$smarty->display('news/list.tpl');
?>
模板list.tpl
{* i am a smarty comment, i don't exist in the compiled output *}
{*
{$pagesupport.total_records}
{$pagesupport.current_page}
{$pagesupport.total_pages}
{$pagesupport.first}
{$pagesupport.prev}
{$pagesupport.next}
{$pagesupport.last}
*}
{$title}
{foreach item=o from=$results}
{$o.news.id} {$o.news.title}
{foreachelse}
娌℃湁鎮ㄨ