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

WBOY
Lepaskan: 2016-07-25 08:52:37
asal
1053 orang telah melayarinya
  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->pagefor ($i=1;$ipage+1;$i++){
  55. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  56. }
  57. }else{
  58. if ($this->pgfor ($i=1;$i$this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  59. }
  60. }else{
  61. if (6+$this->pgpage){
  62. for ($i=$this->pg-4;$ipg+6;$i++){
  63. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  64. }
  65. }else{
  66. for ($i=$this->pg-4;$ipage+1;$i++){
  67. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  68. }
  69. }

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


sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!