Home  >  Article  >  Backend Development  >  Do you know how to implement array paging in php?

Do you know how to implement array paging in php?

PHPz
PHPzforward
2016-05-16 19:53:392652browse

This article mainly introduces the implementation method of PHP array paging, involving PHP array operations, mathematical operations and string operations and other related techniques. Friends in need can refer to

Do you know how to implement array paging in php?

The example in this article describes the implementation method of PHP array paging. Share it with everyone for your reference, the details are as follows:

<?php
$arr_click = array(
array( &#39;clicks&#39; => 3, &#39;clickDate&#39; =>&#39;2010-10-11&#39; ),
array( &#39;clicks&#39; => 2, &#39;clickDate&#39; =>&#39;2010-10-10&#39; ),
array( &#39;clicks&#39; => 3, &#39;clickDate&#39; =>&#39;2010-10-09&#39; ),
array( &#39;clicks&#39; => 4, &#39;clickDate&#39; =>&#39;2010-10-08&#39; ),
array( &#39;clicks&#39; => 5, &#39;clickDate&#39; =>&#39;2010-10-13&#39; ),
array( &#39;clicks&#39; => 7, &#39;clickDate&#39; =>&#39;2010-10-14&#39; ),
array( &#39;clicks&#39; => 6, &#39;clickDate&#39; =>&#39;2010-10-15&#39; ),
array( &#39;clicks&#39; => 9, &#39;clickDate&#39; =>&#39;2010-10-16&#39; ),
);
if(!isset($_GET[&#39;page&#39;]))
{
  $page = 1;
}
else
{
 $page=$_GET[&#39;page&#39;];
}
$size=3;//每页显示的记录数
$pnum = ceil(count($arr_click) / $size); //总页数,ceil()函数用于求大于数字的最小整数
//用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
$newarr = array_slice($arr_click, ($page-1)*$size, $size);
for($i=0;$i<count($newarr);$i++)
{
 echo $newarr[$i][&#39;clickDate&#39;]."<br/>";
}
?>
<?php
if(!isset($_GET[&#39;page&#39;]) || $_GET[&#39;page&#39;]<=1){
?>
<a href="11111111111111.php?page=1">上一页</a>
<?php }else{ ?>
<a href="11111111111111.php?page=<?php echo $page-1;?>">上一页</a>
<?php } ?>
<?php if($_GET[&#39;page&#39;]>=$pnum) {?>
<a href="11111111111111.php?page=<?php echo $pnum;?>">下一页</a>
<?php }else{ ?>
<a href="11111111111111.php?page=<?php echo $page+1;?>">下一页</a>
<?php } ?>

Recommended learning: "PHP Video Tutorial"

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete