PHP array-based paging function instance

Source: Internet
Author: User
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.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.