PHP Array Page code example

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

  2. $page = $_get[' page '];

  3. $r = $this->page ($array, 5, $page);

  4. Print_r ($R);
  5. Exit ();

  6. foreach ($r ["source"] as $s) {

  7. Echo $s;
  8. }

  9. Function page ($array, $pagesize, $current) {

  10. $_return=array ();

  11. $total =ceil (Count ($array)/$pagesize);//ask for total pages

  12. $prev = (($current-1) <=0? "1":($current-1));//Determine the previous page, if the current page is the first page, click Show First page
  13. $next = (($current + 1) >= $total? $total: $current + 1);//Determine the next page, if the current page is the last page, click on the next page to display the last page
  14. $current = ($current > ($total)? ( $total): $current);//Current page if it is greater than the total number of pages, the current page is the last page
  15. $start = ($current-1) * $pagesize;//When pagination is displayed, how many messages should start reading

  16. for ($i = $start; $i < ($start + $pagesize); $i + +) {

  17. Array_push ($_return, $array [$i]);//Put the displayed information into the array $_return
  18. }
  19. $pagearray ["Source"]=$_return;
  20. $pagearray ["Page"]= "first \" >prev next end;//page style can be adjusted according to needs
  21. return $pagearray;
  22. }

Copy Code
  • 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.