Nice php+mysql paging class

WBOY
Release: 2016-07-25 08:52:45
Original
717 people have browsed it
  1. /*****************************
  2. * @Class name: page
  3. * @Parameter: $myde_total - Total number of records
  4. * $myde_size - The number of records displayed on one page
  5. * $myde_page - the current page
  6. * $myde_url - get the current url
  7. * @Function: paging implementation
  8. * @Author: Song Haige
  9. */
  10. class page {
  11. private $myde_total; //Total number of records
  12. private $myde_size; //Number of records displayed on one page
  13. private $myde_page; //Current page
  14. private $myde_page_count; //Total number of pages
  15. private $myde_i; //Number of starting pages
  16. private $myde_en; //Number of ending pages
  17. private $myde_url; //Get the current url
  18. /*
  19. * $show_pages
  20. * The format of page display, the number of pages showing links is 2*$show_pages+1.
  21. * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页]
  22. */
  23. private $show_pages;
  24. public function __construct($myde_total=1,$myde_size=1,$myde_page=1,$myde_url,$show_pages=2){
  25. $this->myde_total = $this->numeric($myde_total);
  26. $this->myde_size = $this->numeric($myde_size);
  27. $this->myde_page = $this->numeric($myde_page);
  28. $this->myde_page_count = ceil($this->myde_total/$this->myde_size);
  29. $this->myde_url = $myde_url;
  30. if($this->myde_total<0) $this->myde_total=0;
  31. if($this->myde_page<1) $this->myde_page=1;
  32. if($this->myde_page_count<1) $this->myde_page_count=1;
  33. if($this->myde_page>$this->myde_page_count) $this->myde_page=$this->myde_page_count;
  34. $this->limit = ($this->myde_page-1)*$this->myde_size;
  35. $this->myde_i=$this->myde_page-$show_pages;
  36. $this->myde_en=$this->myde_page+$show_pages;
  37. if($this->myde_i<1){
  38. $this->myde_en=$this->myde_en+(1-$this->myde_i);
  39. $this->myde_i=1;
  40. }
  41. if($this->myde_en>$this->myde_page_count){
  42. $this->myde_i = $this->myde_i-($this->myde_en-$this->myde_page_count);
  43. $this->myde_en=$this->myde_page_count;
  44. }
  45. if($this->myde_i<1)$this->myde_i=1;
  46. }
  47. //检测是否为数字
  48. private function numeric($num){
  49. if(strlen($num)){
  50. if(!preg_match("/^[0-9]+$/",$num)){
  51. $num=1;
  52. }else{
  53. $num = substr($num,0,11);
  54. }
  55. }else{
  56. $num=1;
  57. }
  58. return $num;
  59. }
  60. //地址替换
  61. private function page_replace($page){
  62. return str_replace("{page}",$page,$this->myde_url);
  63. }
  64. //首页
  65. private function myde_home(){
  66. if($this->myde_page!=1){
  67. return "page_replace(1)."" title="首页">首页";
  68. }else{
  69. return "

    首页

    ";
  70. }
  71. }
  72. //上一页
  73. private function myde_prev(){
  74. if($this->myde_page!=1){
  75. return "page_replace($this->myde_page-1)."" title="上一页">上一页";
  76. }else{
  77. return "

    上一页

    ";
  78. }
  79. }
  80. //下一页
  81. private function myde_next(){
  82. if($this->myde_page!=$this->myde_page_count){
  83. return "page_replace($this->myde_page+1)."" title="下一页">下一页";
  84. }else{
  85. return"

    下一页

    ";
  86. }
  87. }
  88. //尾页
  89. private function myde_last(){
  90. if($this->myde_page!=$this->myde_page_count){
  91. return "page_replace($this->myde_page_count)."" title="尾页">尾页";
  92. }else{
  93. return "

    尾页

    ";
  94. }
  95. }
  96. //输出
  97. public function myde_write($id='page'){
  98. $str ="
    ";
  99. $str.=$this->myde_home();
  100. $str.=$this->myde_prev();
  101. if($this->myde_i>1){
  102. $str.="

    ...

    ";
  103. }
  104. for($i=$this->myde_i;$i<=$this->myde_en;$i++){
  105. if($i==$this->myde_page){
  106. $str.="page_replace($i)."" title="第".$i."页" class="cur">$i";
  107. }else{
  108. $str.="page_replace($i)."" title="第".$i."页">$i";
  109. }
  110. }
  111. if( $this->myde_en<$this->myde_page_count ){
  112. $str.="

    ...

    ";
  113. }
  114. $str.=$this->myde_next();
  115. $str.=$this->myde_last();
  116. $str.="

    ".$this->myde_page_count.

  117. "".$this->myde_total."条数据

    ";
  118. $str.="
";
  • return $str;
  • }
  • }
  • ?>
  • 复制代码

    php+mysql paging principle: limit ($curpage-1)*$showrow,$showrow

    2, php display page code

    1. require_once('./page.class.php'); //Paging class
    2. $showrow = 3;//Number of rows displayed on one page
    3. $curpage = empty($_GET ['page'])?1:$_GET['page'];//The current page should also handle non-numeric situations
    4. $url = "?page={page}";//Paging address, if any Retrieval condition="?page={page}&q=".$_GET['q']
    5. //The code linking mysql is omitted, add it yourself during testing
    6. $sql = "SELECT * FROM table";
    7. $query = mysql_query($sql);
    8. $total = mysql_num_rows($query);//Total number of records
    9. if(!empty($_GET['page']) && $total !=0 && $curpage > ceil($ total/$showrow))
    10. $curpage = ceil($total_rows/$showrow);//The current page number is greater than the last page number, take the last page
    11. //Get the data
    12. $get_data = "select * from table limit ". ($curpage-1)*$showrow.",$showrow;";
    13. ...
    14. ?>
    15. Simple and universal PHP paging class-bbs.it-home.org
    16. < ;body>
  • if($total>$showrow){//The total number of records is greater than the number displayed on each page, display paging
  • $page = new page($total,$showrow,$curpage,$ url,2);
  • echo $page->myde_write();
  • }
  • ?>
  • Copy Code


    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!