当前位置:Gxlcms > PHP教程 > php通用分页类代码,仿goolge分页样式

php通用分页类代码,仿goolge分页样式

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

  1. /**

  2. ** 通用php分页类。(仿Google样式)
  3. ** 只需提供记录总数与每页显示数两个参数。(已附详细使用说明..)
  4. ** 无需指定URL,链接由程序生成。方便用于检索结果分页。
  5. ** 表单采用GET方法提交,可保证在诸如查询,删除之类的操作时,不丢失URL参数。
  6. **/
  7. class Pager{
  8. //IE地址栏地址
  9. var $url;
  10. //记录总条数
  11. var $countall;
  12. //总页数
  13. var $page;
  14. //分页数字链接
  15. var $thestr;
  16. //首页、上一页链接
  17. var $backstr;
  18. //尾页、下一页链接
  19. var $nextstr;
  20. //当前页码
  21. var $pg;
  22. //每页显示记录数量
  23. var $countlist;
  24. //翻页样式
  25. var $style;
  26. //构造函数,实例化该类的时候自动执行该函数
  27. function Pager($countall,$countlist,$style="page"){
  28. //记录数与每页显示数不能整队时,页数取余后加1
  29. $this->countall = $countall;
  30. $this->countlist = $countlist;
  31. $this->style=$style;
  32. if ($this->countall%$this->countlist!=0){
  33. $this->page=sprintf("%d",$this->countall/$this->countlist)+1;
  34. }else{
  35. $this->page=$this->countall/$this->countlist;
  36. }

  37. $this->pg=$_GET["pg"];

  38. //保证pg在未指定的情况下为从第1页开始
  39. if (!ereg("^[1-9][0-9]*$",$this->pg) || empty($this->pg)){
  40. $this->pg=1;
  41. }
  42. //页码超出最大范围,取最大值
  43. if ($this->pg>$this->page){
  44. $this->pg=$this->page;
  45. }
  46. //得到当前的URL。具体实现请看最底部的函数实体
  47. $this->url = Pager::getUrl();
  48. //替换错误格式的页码为正确页码
  49. if(isset($_GET["pg"]) && $_GET["pg"]!=$this->pg){
  50. $this->url=str_replace("?pg=".$_GET["pg"],"?pg=$this->pg",$this->url);
  51. $this->url=str_replace("&pg=".$_GET["pg"],"&pg=$this->pg",$this->url);
  52. }
  53. //生成12345等数字形式的分页。
  54. if ($this->page<=10){
  55. for ($i=1;$i<$this->page+1;$i++){
  56. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  57. }
  58. }else{
  59. if ($this->pg<=5){
  60. for ($i=1;$i<10;$i++){
  61. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  62. }
  63. }else{
  64. if (6+$this->pg<=$this->page){
  65. for ($i=$this->pg-4;$i<$this->pg+6;$i++){
  66. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  67. }
  68. }else{
  69. for ($i=$this->pg-4;$i<$this->page+1;$i++){
  70. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  71. }

  72. }

  73. }
  74. }
  75. //生成上页下页等文字链接
  76. $this->backstr = Pager::gotoback($this->pg);
  77. $this->nextstr = Pager::gotonext($this->pg,$this->page);
  78. //echo (" 共".$this->countall." 条,每页".$this->countlist."条,共".$this->page."页".$this->backstr.$this->thestr.$this->nextstr);
  79. }
  80. //生成数字分页的辅助函数
  81. function makepg($i,$pg){
  82. if ($i==$pg){
  83. return " ".$i."";
  84. }else{
  85. return " url,5,$i)." class='".$this->style."'>".$i."";
  86. }
  87. }
  88. //生成上一页等信息的函数
  89. function gotoback($pg){
  90. if ($pg-1>0){
  91. return $this->gotoback=" url,3,0)." class='".$this->style."'>首页 url,2,0)." class='".$this->style."'>上一页";
  92. }else{
  93. return $this->gotoback="首页 上一页 ";
  94. }
  95. }
  96. //生成下一页等信息的函数
  97. function gotonext($pg,$page){
  98. if ($pg < $page){
  99. return " url,1,0)." class='".$this->style."'>下一页 url,4,0)." class='".$this->style."'>尾页";
  100. }else{
  101. return " 下一页 尾页";
  102. }
  103. } bbs.it-home.org
  104. //处理url中$pg的方法,用于自动生成pg=x
  105. function replacepg($url,$flag,$i){
  106. if ($flag == 1){
  107. $temp_pg = $this->pg;
  108. return str_replace("pg=".$temp_pg,"pg=".($this->pg+1),$url);
  109. }else if($flag == 2) {
  110. $temp_pg = $this->pg;
  111. return str_replace("pg=".$temp_pg,"pg=".($this->pg-1),$url);
  112. }else if($flag == 3) {
  113. $temp_pg = $this->pg;
  114. return str_replace("pg=".$temp_pg,"pg=1",$url);
  115. }else if($flag == 4){
  116. $temp_pg = $this->pg;
  117. return str_replace("pg=".$temp_pg,"pg=".$this->page,$url);
  118. }else if($flag == 5){
  119. $temp_pg = $this->pg;
  120. return str_replace("pg=".$temp_pg,"pg=".$i,$url);
  121. }else{
  122. return $url;
  123. }
  124. }
  125. //获得当前URL的方法
  126. function getUrl(){
  127. $url="http://".$_SERVER["HTTP_HOST"];
  128. if(isset($_SERVER["REQUEST_URI"])){
  129. $url.=$_SERVER["REQUEST_URI"];
  130. }else{
  131. $url.=$_SERVER["PHP_SELF"];
  132. if(!empty($_SERVER["QUERY_STRING"])){
  133. $url.="?".$_SERVER["QUERY_STRING"];
  134. }
  135. }
  136. //在当前的URL里加入pg=x字样
  137. if (!ereg("(pg=|PG=|pG=|Pg=)", $url)){
  138. if (!strpos($url,"?")){
  139. $url = $url."?pg=1";
  140. }else{
  141. $url = $url."&pg=1";
  142. }
  143. }
  144. return $url;
  145. }
  146. }
  147. ?>

人气教程排行