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

Source: Internet
Author: User

  1. <?php
  2. /**
  3. * Number of Components page function core function Array_slice
  4. * Before using this function, all the data inside the database must be queried in a certain order to be stored in the array.
  5. * $count How many data per page
  6. * $page Current Page
  7. * $array all the arrays queried
  8. * Order 0-Invariant 1-Reverse order
  9. */
  10. function Page_array ($count,$page,$array,$order) {
  11. global $countpage; #定全局变量
  12. $page = (empty ($page))?' 1 ':$page; #判断当前页面是否为空 if it is empty, it is represented as the first page
  13. $start = ($page-1) *$count; #计算每次分页的开始位置
  14. if ($order ==1) {
  15. $array =array_reverse ($array);
  16. }
  17. $totals =count ($array);
  18. $countpage =ceil ($totals/$count); #计算总页面数
  19. $pagedata =Array ();
  20. $pagedata =array_slice ($array,$start,$count);
  21. return $pagedata; #返回查询数据
  22. }
  23. /**
  24. * Paging and display functions
  25. * $countpage Global variables, write
  26. * $url Current URL
  27. */
  28. function Show_array ($countpage,$url) {
  29. $page =empty ($_get[' page ')? 1:$_get[' page ';
  30. if ($page > 1) {
  31. $uppage =$page-1;
  32. }else{
  33. $uppage = 1;
  34. }
  35. if ($page < $countpage) {
  36. $nextpage =$page +1;
  37. }else{
  38. $nextpage =$countpage;
  39. }
  40. $str =' <div style= ' border:1px; width:300px; height:30px; color: #9999CC ">";
  41. $str. ="<span> Total {$countpage} page/{$page} page </span>";
  42. $str. ="<span><a href= ' $url? page=1 ' > Home </a></span>";
  43. $str. ="<span><a href= ' $url? page={$uppage} ' > Prev </a></span>";
  44. $str. ="<span><a href= ' $url? page={$nextpage} ' > Next </a></span>";
  45. $str. ="<span><a href= ' $url? page={$countpage} ' > Last </a></span>";
  46. $str. =' </div> ';
  47. return $str;
  48. }
  49. ?>

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

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.