This article mainly introduces the array-based paging function example of PHP, which is a very practical technique. For more information, see
This article mainly introduces the array-based paging function example of PHP, which is a very practical technique. For more information, see
The paging function is a very common function in PHP programming. Unlike previous functions, this article introduces the array-based paging function of PHP.
With regard to the array paging function, the advantage of using Arrays for paging is that it is convenient to perform Combined Multi-table queries, you only need to put the query results in an array. The following is an array paging function. The page_array function is used for Array paging. The show_array function is used for paging function operation and display. It must be used in combination. The two functions are linked through the global variable $ countpage, which is used to track the total number of page numbers.
The specific instance code is as follows:
<? Php/*** array paging function core function array_slice * before using this function, you must first query all the data in the database in a certain order and store the data in the array * $ count how many entries per page data * $ page Current page * $ all arrays queried by array * order 0-unchanged 1-Reverse order */function page_array ($ count, $ page, $ array, $ order) {global $ countpage; # set the global variable $ page = (empty ($ page ))? '1': $ page; # judge whether the current page is empty. If it is null, it indicates the first page $ start = ($ page-1) * $ count; # Calculate the start position of each page. if ($ order = 1) {$ array = array_reverse ($ array) ;}$ totals = count ($ array ); $ countpage = ceil ($ totals/$ count); # calculate the total number of pages $ pagedata = array (); $ pagedata = array_slice ($ array, $ start, $ count ); return $ pagedata; # return query data}/*** paging and display function ** $ countpage global variable, write ** $ url current url */function show_array ($ countpage, $ url) {$ page = empty ($ _ GET ['page'])? 1: $ _ GET ['page']; if ($ page> 1) {$ uppage = $ page-1;} else {$ uppage = 1 ;} if ($ page <$ countpage) {$ nextpage = $ page + 1;} else {$ nextpage = $ countpage;} $ str ='
'; $ Str. = "total page {$ countpage}/page {$ page}"; $ str. = "Homepage"; $ str. = "Previous Page"; $ str. = "next page"; $ str. = "last page"; $ str. ='
'; Return $ str ;}?>
We hope that the examples described in this article will provide some reference for everyone's PHP programming.
,