查看: 5439|回复: 0
打印 上一主题 下一主题

PHP实例源代码:PHP实现翻页处理

[复制链接]
跳转到指定楼层
1#
发表于 2007-10-5 15:48:23 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
  1. <?php
  2. class Page{

  3. var $CountAll; //共有纪录数
  4. var $CountPage; //每页显示记录数
  5. var $Link; //显示 完整的分页信息
  6. var $ForPage; //上一页
  7. var $NextPage; //下一页
  8. var $FirstPage; //第一页
  9. var $LastPage; //最后一页
  10. var $CurrPage; //第几页
  11. var $PageNum; //共有多少页
  12. var $Parameter; //参数
  13. var $LimitNum; //不是统计全部记录,而是显示部分记录,例如共有100条记录,但是只统计显示前50条

  14. function Page($sql, $num=30){

  15. //初始化,统计记录数
  16. $this->CountPage = $num;
  17. global $mysql;
  18. $sql = base64_decode($sql);
  19. $result = $mysql->Query($sql);
  20. if (0 != $mysql->AffectedRows()){
  21. $row = $mysql->FetchArray($result);
  22. $this->CountAll = $row[0];
  23. }
  24. else{
  25. $this->CountAll = 0;
  26. }
  27. //print "共有 $this->CountAll
  28. ";
  29. }

  30. function ListPage($sql, $page=0,$sql_all,$other){
  31. //查询,定义变量,获取数据

  32. global $mysql;
  33. //print "sql sql
  34. ";
  35. if (isset($this->LimitNum) && $this->CountAll > $this->LimitNum){
  36. $this->CountAll = $this->LimitNum;
  37. }//更新总浏览记录数

  38. $sql_src = $sql;
  39. //if ($page > 0){
  40. $sql = base64_decode($sql);
  41. $sql_all = base64_decode($sql_all);
  42. $sql_src = $sql;
  43. //}
  44. //echo $sql;
  45. if (($this->CountAll % $this->CountPage) == 0)//统计共有多少页
  46. $pagecount = (integer)($this->CountAll/$this->CountPage);
  47. else
  48. $pagecount = (integer)($this->CountAll/$this->CountPage)+1;
  49. $this->ageNum = $pagecount;
  50. if ($page > $this->ageNum)//如果页码超过页码总数则设为最大页码
  51. $page = $this->ageNum;
  52. if ($page <= 0)//如果页码小于等于零则将页码设置为1
  53. $page = 1;

  54. if ($this->CountAll == 0)
  55. {
  56. $this->CurrPage = 0;
  57. }else{
  58. $this->CurrPage = $page;
  59. }
  60. $first_start = ($page-1)*$this->CountPage;
  61. $sql = $sql." limit ".$first_start.", ".$this->CountPage;
  62. //print "2sql
  63. ";
  64. $result = $mysql->Query($sql);
  65. if (0 != $mysql->AffectedRows()){
  66. $i = 0;
  67. while($row = $mysql->FetchArray($result)){
  68. $array[$i] = $row;
  69. //print "name:".$array[$i][Name]."
  70. ";
  71. $i++;
  72. }
  73. }

  74. $sql = base64_encode($sql_src);
  75. $sql_all = base64_encode($sql_all);
  76. if ($pagecount >1){
  77. if($page == 1){
  78. $nextpage = $page+1;
  79. $forpage = 1;
  80. $this->Link = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  81. $this->NextPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  82. $this->LastPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  83. }
  84. else if(($page > 1)&&($page < $pagecount)) {
  85. $forpage = $page-1;
  86. $nextpage = $page+1;
  87. $this->Link = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  88. $this->ForPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  89. $this->NextPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  90. $this->FirstPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  91. $this->LastPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  92. }
  93. else if ($page = $pagecount){
  94. $forpage = $page-1;
  95. $nextpage = 1;
  96. $this->Link = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  97. $this->FirstPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  98. $this->ForPage = "Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";> ";
  99. }
  100. }
  101. else{
  102. $this->Link = ' ';
  103. }

  104. return $array;//$array;

  105. }

  106. }
  107. /***************
  108. 使用例子

  109. include("class.config.php");
  110. include("class.mysql.php");
  111. include("class.page.php");
  112. global $mysql;
  113. $config = new Config;
  114. $mysql = new TDatabase($config);

  115. $query_all = "select count(*) from user";
  116. $page_object = new Page($query_all,20);
  117. //new Page('统计记录个数语句',每页记录个数)
  118. if(empty($query_page))
  119. $query_sql = "select * from user";
  120. //注意这里的变量名必须为 $query_sql $query_page ,因为下一页的连接参数默认为 query_sql query_page

  121. $list = $page_object->ListPage($query_sql,$query_page);
  122. //ListPage('没有limit的前一部分,系统自动根据补齐',察看的页数)
  123. $page_object->Parameter = '&action=view';
  124. //这是传送的Url 所带的其它参数,如果有就修改变量 Parameter ,系统自动将她补在后面
  125. //显示数据
  126. for ($i=0;$i< $page_object->CountPage;$i++)
  127. print $list[$i][ID]."->".$list[$i][UserName]."
  128. ";
  129. //返回的数据为二维哈西(关联)数组,一维为纪录的标识ID号,二维为哈西(关联)数组,取值标识建议采取用数据库中字段名的方法,例如list[0][UserName]。

  130. //显示其他相关数据
  131. echo $page_object->CountAll;//纪录总数
  132. echo $page_object->CountPage;//每页显示数据个数
  133. echo $page_object->Link;//显示完整的分页信息
  134. echo $page_object->FirstPage;//第一页
  135. echo $page_object->NextPage;//下一页
  136. echo $page_object->ForPage;//上一页
  137. echo $page_object->LastPage;//最后一页
  138. echo $page_object->CurrPage;//第几页
  139. echo $page_object->PageNum;//共有多少页
  140. $mysql->DatabaseClose();
  141. ****************/

  142. ?>
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表