对数据进行分页 PHP

$array=array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20");
$page = isset($_GET["page"]) ? $_GET["page"] : 1;
$r 	  = page($array,"5",$page);
foreach($r["source"] as $s){
	echo $s;
}
echo $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"]="首页 上一页 下一页 最后一页";

	return $pagearray;
}

 

你可能感兴趣的:(PHP)