当前位置:Gxlcms > PHP教程 > php数组array函数实现php分页

php数组array函数实现php分页

时间:2021-07-01 10:21:17 帮助过:17人阅读

  1. //php分页代码
  2. $array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");//规定要分页的数组
  3. $page= $_GET['page'];
  4. $r = $this->page($array,5,$page);
  5. print_r($r);
  6. exit();
  7. foreach($r["source"] as $s){
  8. echo $s;
  9. }
  10. function page($array,$pagesize,$current){
  11. $_return=array();
  12. $total=ceil(Count($array)/$pagesize);//求总页数
  13. $prev=(($current-1)<=0 ? "1":($current-1));//确定上一页,如果当前页是第一页,点击显示第一页
  14. $next=(($current+1)>=$total ? $total:$current+1);//确定下一页,如果当前页是最后一页,点击下页显示最后一页
  15. $current=($current>($total)?($total):$current);//当前页如果大于总页数,当前页为最后一页
  16. $start=($current-1)*$pagesize;//分页显示时,应该从多少条信息开始读取
  17. for($i=$start;$i<($start+$pagesize);$i++){
  18. array_push($_return,$array[$i]);//将该显示的信息放入数组 $_return 中
  19. }
  20. $pagearray["source"]=$_return;
  21. $pagearray["page"]="first \">prev next end";//分页样式可跟据需要调整
  22. return $pagearray;
  23. }

人气教程排行