/**
* 翻页使用 大数据order 翻转limit算法 因为sql排序翻转了 所以在外部数据要自己翻转回来 用array_reverse()即可
* @param int $total 数据总数
* @param int $cut_start 截取开始 例如sql里的 limit 10,20 $cut_start就是10
* @param int $cut_num 截取个数 limit 10,20 $cut_num 就是20
* @param bool $desc 排序方式是倒叙 区分倒叙和正序
* @return bool or [int 截取开始数,int 截取个数,string 倒叙正序]
*/
function big_limit_handle($total,$cut_start,$cut_num,$desc=true)
{
$mid = floor ( $total / 2 );
//如果大于间隔 则翻转
if ( $cut_start > $mid ) {
$desc = !$desc;
}else{
return false;
}
//下面是翻转算法
$h_limit = $total - ( $cut_start + $cut_num );
$h_cut_num=$cut_num;
//如果小于0说明是最后一页
if($h_limit<0){
$h_limit=$cut_num+$h_limit;
$h_cut_num=$h_limit;
$h_limit=0;
}
if ( $desc ) {
$order = 'desc';
} else {
$order = 'asc';
}
$return = array(
'limit' => $h_limit,
'h_cut_num'=>$h_cut_num,
'order' => $order ,
);
return $return;
}
用法
$sql = "SELECT count(*) as num FROM `comment_data` c $join where " . $where;
$this->comment_data_db->query ( $sql );
$num = $this->comment_data_db->fetch_array ();
$total=( isset( $num[ 0 ][ 'num' ] ) ? $num[ 0 ][ 'num' ] : 0 );
$cut_num=20;
$cache_data[ 'pages' ] = pages ( $total , $page , $cut_num );
$limit=( $page - 1 ) * $cut_num;
/*翻转算法start*/
$big_limit_handle=big_limit_handle($total,$limit,$cut_num,true);
$id_order='DESC';
if($big_limit_handle){
$id_order=$big_limit_handle['order'];
$limit=$big_limit_handle['limit'];
$cut_num=$big_limit_handle['h_cut_num'];
}
/*翻转算法end*/
$where = "SELECT $fields FROM `comment_data` c $join where " . $where . " ORDER BY {$support_desc}id $id_order limit " . $limit . ",$cut_num";
$this->comment_data_db->query ( $where );
$cache_data[ 'data' ] = $this->comment_data_db->fetch_array ();
if($big_limit_handle){
$cache_data[ 'data' ]=array_reverse($cache_data[ 'data' ]);
}