欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php array 分页代码示例

程序员文章站 2024-01-20 15:11:52
...
  1. $array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");//规定要分页的数组

  2. $page= $_GET['page'];
  3. $r = $this->page($array,5,$page);

  4. print_r($r);
  5. exit();
  6. foreach($r["source"] as $s){

  7. echo $s;
  8. }
  9. function page($array,$pagesize,$current){

  10. $_return=array();
  11. $total=ceil(Count($array)/$pagesize);//求总页数

  12. $prev=(($current-1) $next=(($current+1)>=$total ? $total:$current+1);//确定下一页,如果当前页是最后一页,点击下页显示最后一页
  13. $current=($current>($total)?($total):$current);//当前页如果大于总页数,当前页为最后一页
  14. $start=($current-1)*$pagesize;//分页显示时,应该从多少条信息开始读取
  15. for($i=$start;$i array_push($_return,$array[$i]);//将该显示的信息放入数组 $_return 中

  16. }
  17. $pagearray["source"]=$_return;
  18. $pagearray["page"]="first \">prev next end";//分页样式可跟据需要调整
  19. return $pagearray;
  20. }
复制代码