php数组分页

  作者:会飞的

<?php$array=array("1","2","3","4","5","6","7","8","9","10","11","12"

<?php

$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");

if(empty($_GET['page'])){

  $page=1;

}else{

  $page=$_GET["page"];

}

$r=page($array,"5",$page);

echo "<table><tr><td>bianhao</td></tr>";

foreach($r["source"] as $s){

echo "<tr><td>";

echo $s;

echo "</td></tr>";

}

echo "</table>";

echo "<br/>".$r["page"];



function page($array,$pagesize,$current){

$_return=array();

/*calculator*/

$total=ceil(Count($array)/$pagesize);

$prev=(($current-1)<=0 ? "1":($current-1));

$next=(($current+1)>=$total ?   $total:$current+1);


$current=($current>($total)?($total):$current);


$start=($current-1)*$pagesize;



for($i=$start;$i<($start+$pagesize);$i++){

array_push($_return,$array[$i]);

}

$pagearray["source"]=$_return;

$pagearray["page"]="<a href="?page=1">first</a> <a href="?page={$prev}">prev</a> <a href="?page={$next}">next</a> <a href="?page={$total}">end</a>";

return $pagearray;

}

?>