一个php+mysql分页类代码

WBOY
发布: 2016-07-25 08:52:47
原创
1227 人浏览过
  1. /************************************

  2. 类名: pagesupport
  3. 功能:分页显示mysql数据库中的数据
  4. *************************************/
  5. class pagesupport{
  6. //属性
  7. var $sql;//所要显示数据的SQL查询语句
  8. var $page_size;//每页显示最多行数
  9. var $start_index;//所要显示记录的首行序号
  10. var $total_records;//记录总数
  11. var $current_records;//本页读取的记录数
  12. var $result;//读出的结果
  13. var $total_pages;//总页数
  14. var $current_page;//当前页数
  15. var $display_count = 30; //显示的前几页和后几页数
  16. var $arr_page_query; //数组,包含分页显示需要传递的参数
  17. var $first;
  18. var $prev;
  19. var $next;
  20. var $last;
  21. //方法
  22. /*********************************************
  23. 构造函数:__construct()
  24. 输入参数:
  25. $ppage_size:每页显示最多行数
  26. ***********************************************/
  27. function PageSupport($ppage_size)
  28. {
  29. $this->page_size=$ppage_size;
  30. $this->start_index=0;
  31. }
  32. /*********************************************

  33. 构造函数:__destruct()
  34. 输入参数:
  35. ***********************************************/
  36. function __destruct()
  37. {
  38. }
  39. /*********************************************
  40. get函数:__get()
  41. ***********************************************/
  42. function __get($property_name)
  43. {
  44. if(isset($this->$property_name))
  45. {
  46. return($this->$property_name);
  47. }
  48. else
  49. {
  50. return(NULL);
  51. }
  52. }
  53. /*********************************************
  54. set函数:__set()
  55. ***********************************************/
  56. function __set($property_name, $value)
  57. {
  58. $this->$property_name = $value;
  59. }
  60. /*********************************************
  61. 函数名:read_data
  62. 功能:根据SQL查询语句从表中读取相应的记录
  63. 返回值:属性二维数组result[记录号][字段名]
  64. ***********************************************/
  65. function read_data()
  66. {
  67. $psql=$this->sql;
  68. //查询数据,数据库链接等信息应在类调用的外部实现
  69. $result=mysql_query($psql) or die(mysql_error());
  70. $this->total_records=mysql_num_rows($result);
  71. //利用LIMIT关键字获取本页所要显示的记录
  72. if($this->total_records>0)
  73. {
  74. $this->start_index = ($this->current_page-1)*$this->page_size;
  75. $psql=$psql." LIMIT ".$this->start_index." , ".$this->page_size;
  76. $result=mysql_query($psql) or die(mysql_error());
  77. $this->current_records=mysql_num_rows($result);
  78. //将查询结果放在result数组中
  79. $i=0;
  80. while($row=mysql_fetch_Array($result))
  81. {
  82. $this->result[$i]=$row;
  83. $i++;
  84. }
  85. }
  86. //获取总页数、当前页信息
  87. $this->total_pages=ceil($this->total_records/$this->page_size);
  88. $this->first=1;
  89. $this->prev=$this->current_page-1;
  90. $this->next=$this->current_page+1;
  91. $this->last=$this->total_pages;
  92. }
  93. /*********************************************
  94. 函数名:standard_navigate()
  95. 功能:显示首页、下页、上页、未页
  96. ***********************************************/
  97. function standard_navigate()
  98. {
  99. echo "
    ";
  100. echo "
    ";
  101. echo "第".$this->current_page."页/共".$this->total_pages."页";
  102. echo "";
  103. echo "跳到页";
  104. echo "";
  105. //生成导航链接
  106. if ($this->current_page > 1) {
  107. echo "first.">首页|";
  108. echo "prev.">上一页|";
  109. }
  110. if( $this->current_page total_pages) {
  111. echo "next.">下一页|";
  112. echo "last.">末页";
  113. }
  114. echo "
  115. ";
  116. echo "
";
  • }
  • /*********************************************
  • 函数名:full_navigate()
  • 功能:显示首页、下页、上页、未页
  • 生成导航链接 如1 2 3 ... 10 11
  • ***********************************************/
  • function full_navigate()
  • {
  • echo "
    ";
  • echo "
    ";
  • echo "第".$this->current_page."页/共".$this->total_pages."页";
  • echo "";
  • echo "跳到页";
  • echo "";
  • //生成导航链接 如1 2 3 ... 10 11
  • $front_start = 1;
  • if($this->current_page > $this->display_count){
  • $front_start = $this->current_page - $this->display_count;
  • }
  • for($i=$front_start;$icurrent_page;$i++){
  • echo "[".$i ."] ";
  • }
  • echo "[".$this->current_page."]";
  • $displayCount = $this->display_count;
  • if($this->total_pages > $displayCount&&($this->current_page+$displayCount)total_pages){
  • $displayCount = $this->current_page+$displayCount;
  • }else{
  • $displayCount = $this->total_pages;
  • }
  • for($i=$this->current_page+1;$iecho "[".$i ."] ";
  • }
  • //生成导航链接
  • if ($this->current_page > 1) {
  • echo "first.">首页|";
  • echo "prev.">上一页|";
  • }
  • if( $this->current_page total_pages) {
  • echo "next.">下一页|";
  • echo "last.">末页";
  • }
  • echo "
  • ";
  • echo "
  • ";
  • }
  • }
  • ?>
  • 复制代码

    分页类用法:

    1. include_once("fenye_php.php"); //引入类
    2. ///////////////////////////////////////////////////////////////////////
    3. $con = mysql_connect("localhost","root","");
    4. if (!$con)
    5. {
    6. die('Could not connect: ' . mysql_error());
    7. }
    8. mysql_select_db("myblog", $con);//选取数据库

    9. $PAGE_SIZE=10;//设置每页显示的数目
    10. ///////////////////////////////////////////////////////////////////////
    11. $pageSupport = new PageSupport($PAGE_SIZE); //实例化PageSupport对象
    12. $current_page=$_GET["current_page"];//分页当前页数
    13. if (isset($current_page)) {
    14. $pageSupport->__set("current_page",$current_page);
    15. } else {
    16. $pageSupport->__set("current_page",1);
    17. }
    18. $pageSupport->__set("sql","select * from article ");
    19. $pageSupport->read_data();//读数据
    20. if ($pageSupport->current_records > 0) //如果数据不为空,则组装数据
    21. {
    22. for ($i=0; $icurrent_records; $i++)
    23. {
    24. $title = $pageSupport->result[$i]["title"];
    25. $content = $pageSupport->result[$i]["content"];
    26. $part=substr($content,0,400);
    27. //循环输出每条数据
    28. echo '
    29. '.$title.'
    30. '.$part.'
    31. update delet
  • ';
  • }
  • }
  • $pageSupport->standard_navigate(); //调用类里面的这个函数,显示出分页HTML
  • //关闭数据库
  • mysql_close($con);
  • ?>
  • 复制代码


  • 来源:php.cn
    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
    最新问题
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责声明 Sitemap
    PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!