PHP array-based paging function (core function array_slice ())

Source: Internet
Author: User

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. two functions are linked by the global variable $ countpage. This variable is used to track the total number of page numbers.

 

<? 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 = '<Div style = "border: 1px; width: 300px; Height: 30px; color: # 9999cc "> '; $ Str. = "<span> total page {$ countpage}/Page {$ page} </span>"; $ Str. = "<span> <a href = '$ URL? Page = 1 '> homepage </a> </span> "; $ Str. =" <span> <a href =' $ URL? Page = {$ uppage} '> previous page </a> </span> "; $ Str. =" <span> <a href =' $ URL? Page = {$ nextpage} '> next page </a> </span> "; $ Str. =" <span> <a href =' $ URL? Page = {$ countpage} '> last page </a> </span> "; $ Str. =' </div> '; return $ STR ;}?>

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.