Php array paging, array Paging

Source: Internet
Author: User

Php array paging, array Paging

$ Array = array ("1", "2", "3", "4", "5", "6", "7", "8 ", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18 ", "19", "20"); // specifies the array to be paged

$ Page = $ _ GET ['page'];

$ R = $ this-> page ($ array, 5, $ page );
Print_r ($ r );
Exit ();


Foreach ($ r ["source"] as $ s ){
Echo $ s;
}

 

Function page ($ array, $ pagesize, $ current ){
$ _ Return = array ();

$ Total = ceil (Count ($ array)/$ pagesize); // calculates the total number of pages
$ Prev = ($ current-1) <= 0? "1" :( $ current-1); // confirm the previous page. If the current page is the first page, click to display the first page.
$ Next = ($ current + 1) >=$ total? $ Total: $ current + 1); // confirm the next page. If the current page is the last page, click the next page to display the last page.
$ Current = ($ current> ($ total )? ($ Total): $ current); // if the current page is greater than the total page number, the current page is the last page
$ Start = ($ current-1) * $ pagesize; // the number of items that should be read from the page.

For ($ I = $ start; $ I <($ start + $ pagesize); $ I ++ ){
Array_push ($ _ return, $ array [$ I]); // put the displayed information into the array $ _ return
}
$ Pagearray ["source"] =$ _ return;
$ Pagearray ["page"] = "<a href = \"? Page = 1 \ "> first </a> <a href = \"? Page = {$ prev}
\ "> Prev </a> <a href = \"? Page = {$ next }\ "> next </a> <a href = \"? Page = {$ total }\ "> end </a>"; // The page style can be adjusted as needed
Return $ pagearray;
}


Array $ array [] in php display by PAGE

$ ListArr = array_slice ($ array, ($ page-1) * $ pagesize, $ pagesize, true );
$ Page indicates the number of pages.
 
Pagination of php Arrays

We recommend that you do not use php paging because the data is complete. You can use js Paging for the purpose of optimizing the display effect. This is not recommended because page Jump requires a long wait for visitors.

Usually, the database content is displayed on pages. For example, you need to display 100 entries, but only 10 entries are displayed at a time. One is faster display, and the other is mainly because the pressure on the database is reduced.

You can simply put them in multiple divs and control the display and hiding of divs on the next page of the previous page.
 

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.