脚本宝典收集整理的这篇文章主要介绍了php – 如何从数组做分页?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$display_array = Array ( [0] => "0602 xxx2",[1] => "0602 xxx3",[2] => 5 // total= 2+3 [3] => "0602 xxx3",[4] => "0602 saa4",[5] => 7 // Total = 3+4 )
我尝试过这样的事情
function pagination($display_array,$page) { global $show_PEr_page; $page = $page < 1 ? 1 : $page; $start = ($page - 1) * $show_per_page; $end = $page * $show_per_page; for($i = $start; $i < $end; $i++) { ////echo $display_array[$i] . "<p>"; // How to manipulate this? // To get the result as I described below. } }
如果我定义$show_per_page = 2;然后分页($display_array,1);输出:
0602 xxx2 0602 xxxx3 Total:5
和异教徒($display_array,2);输出:
0602 xxx3 0602 saa4 Total:7
如果我定义$show_per_page = 3,那么分页($display_array,1);输出:
0602 xxx2 0602 xxxx3 Total: 5 0602 xxx3
和异教徒($display_array,2);输出:
0602 saa4 Total:7
如果我定义$show_per_page = 4;输出:
0602 xxx2 0602 xxxx3 Total:5 0602 xxx3 0602 saa4 Total: 7
function paganation($display_array,$page) { global $show_per_page; $page = $page < 1 ? 1 : $page; // start posITion in the $display_array // +1 is to account for total values. $start = ($page - 1) * ($show_per_page + 1); $offset = $show_per_page + 1; $outArray = array_slice($display_array,$start,$offset); var_dump($outArray); } $show_per_page = 2; paganation($display_array,1); paganation($display_array,2); $show_per_page = 3; paganation($display_array,2);
输出为:
// when $show_per_page = 2; array 0 => string '0602 xxx2' (length=9) 1 => string '0602 xxx3' (length=9) 2 => int 5 array 0 => string '0602 xxx3' (length=9) 1 => string '0602 saa4' (length=9) 2 => int 7 // when $show_per_page = 3; array 0 => string '0602 xxx2' (length=9) 1 => string '0602 xxx3' (length=9) 2 => int 5 3 => string '0602 xxx3' (length=9) array 0 => string '0602 saa4' (length=9) 1 => int 7
$show_per_page = 3的输出与您的输出不同,但我不知道您期望什么?你想获取剩下的一切(即’0602 saa4’和7)加上一个以前的元素(即’0602 xxx3′)?
以上是脚本宝典为你收集整理的php – 如何从数组做分页?全部内容,希望文章能够帮你解决php – 如何从数组做分页?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。