当前位置:Gxlcms > PHP教程 > php+mysql分页类

php+mysql分页类

时间:2021-07-01 10:21:17 帮助过:34人阅读

封装的类:
  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 "";
  102. }
  103. /*********************************************
  104. 函数名:full_navigate()
  105. 功能: 显示首页、下页、上页、未页
  106. 生成导航链接 如1 2 3 ... 10 11
  107. ***********************************************/
  108. function full_navigate()
  109. {
  110. echo "";
  111. echo "";
  112. echo "";
  113. }
  114. }
  115. ?>

写在php页面里面的代码:
  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; $i<$pageSupport->current_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
  32. ';
  33. }
  34. }
  35. $pageSupport->standard_navigate(); //调用类里面的这个函数,显示出分页HTML
  36. //关闭数据库
  37. mysql_close($con);
  38. ?>

来自:http://blog.csdn.net/phpfenghuo/article/details/23207099
分页, php, mysql

人气教程排行