数组分页去数据

时间:2021-06-24 11:25:41

今天遇到这种情况,图书的ID都存在cookie中,获得的cookie值是

"537975,467452,556978,464954,2531,1878,457036,3431,469552,537693,465265,462728,484600,561234,559843,554795,"

而产品经理还要求分页,以前的分页都是对数据库的分页操作。下面是对数据的分页

#获得的当前页数
$page       = $_GET('page')?$_GET('page'):1;
#每页数量
$pageNum    = 5;
#获得cookie值
$playCookie = "537975,467452,556978,464954,2531,1878,457036,3431,469552,537693,465265,462728,484600,561234,559843,554795,"
#分割数组
$playCookie = explode(',', trim($playCookie,','));
$count      = count($playCookie);
$start      = (page-1)*$pageNum;
$end        = $start + $pageNum;
#重新定义一个数组
$play       = array();
if ($end > $count) {
  $end      = $count; 
}
#分页数据放到新的数组中
for($i=$start; $i < $end; $i++) {
  $play[]   = $playCookie[$i];
}