Paging function is a very common function of PHP programming, unlike in the past, today This article describes PHP is based on the array implementation of the paging function.
About the paging function of an array, the advantage of using an array for paging is that it is convenient to combine multiple table queries, just put the results of the query in an array. The following is the function of array paging, function Page_array for array paging, function Show_array for paging function operation and display, Need to be used in conjunction. Two functions are contacted through global variable $countpage, which is used to track the total number of pages.
The specific example code is as follows:
<?php/** * Number of Components page function core function Array_slice * Use this function to first of all the data in the database in a certain order to query out into the array * $count how many pieces of data per page * $page Current page * $arra Y query out all arrays * Order 0-Invariant 1-Reverse sequence/function Page_array ($count, $page, $array, $order) {global $countpage; #定全局变量 $ Page= (Empty ($page))? ' 1 ': $page; #判断当前页面是否为空 If NULL is the first page $start = ($page-1) * $COUNT;
#计算每次分页的开始位置 if ($order ==1) {$array =array_reverse ($array);
$totals =count ($array); $countpage =ceil ($totals/$count);
#计算总页面数 $pagedata =array ();
$pagedata =array_slice ($array, $start, $count); return $pagedata; #返回查询数据}/** * Paging and display function * $countpage global variable, write * $url current URL/function Show_array ($countpage, $url) {$page =empty ($_ge
t[' page '])? 1:$_get[' page '];
if ($page > 1) {$uppage = $page-1;
}else{$uppage = 1;
} if ($page < $countpage) {$nextpage = $page +1;
}else{$nextpage = $countpage; } $str = ' <div style= ' border:1px; width:300px; height:30px;
Color: #9999CC ">"; $str. = "<span> Total {$countpage} page/{$page} page </SPAN≫ ";
$str. = "<span><a href= ' $url" page=1 ' > Home </a></span> ";
$str. = "<span><a href= ' $url? page={$uppage} ' > Prev </a></span> ';
$str. = "<span><a href= ' $url? page={$nextpage} ' > Next </a></span> ';
$str. = "<span><a href= ' $url? page={$countpage} ' > Last </a></span> ';
$str. = ' </div> ';
return $str;
}?>
I hope that the example described in this article will play a reference role in the PHP program design for everyone.