ホームページ  >  記事  >  バックエンド開発  >  Google ページング スタイルを模倣した PHP の一般的なページング クラス コード

Google ページング スタイルを模倣した PHP の一般的なページング クラス コード

WBOY
WBOYオリジナル
2016-07-25 08:52:371065ブラウズ
  1. /**

  2. ** 一般的な PHP ページング クラス。 (Google スタイルを模倣)
  3. ** レコードの総数とページごとの表示数の 2 つのパラメーターを指定するだけです。 (詳細な説明書は添付されています。)
  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 $backstr;
  16. //最後のページ、次のページのリンク
  17. ;
  18. //現在のページ番号
  19. var $pg;
  20. //各ページに表示されるレコード数
  21. var $countlist;
  22. //このクラスをインスタンス化するときに自動的に実行されるコンストラクターfunction
  23. function Pager($countall,$countlist,$style="page"){
  24. //レコード数と各ページに表示される件数が統合できない場合、ページ数に1を加算した余りとなります
  25. $ this->countall = $countall;
  26. $this->countlist = $countlist;
  27. if ($this->countall%$this->countlist;=0 ){
  28. $this->page=sprintf("%d",$this->countall/$this->countlist)+1;
  29. }else{
  30. $this->page=$this-> ;countall/$this-> ;countlist;
  31. }

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

  33. //指定されていない場合、ページは 1 ページから始まることを保証します
  34. if ( !ereg("^[1-9][0-9]*$",$this->pg) || empty($this->pg)){
  35. $this->pg=1 ;
  36. }
  37. //ページ番号が最大範囲を超えているため、最大値を取得します
  38. if ($this->pg>$this->page){
  39. $this->pg=$this->page ;
  40. }
  41. // 現在の URL を取得します。特定の実装については、下部の関数エンティティを参照してください
  42. $this->url = Pager::getUrl();
  43. //間違った形式のページ番号を正しいページ番号に置き換えます
  44. if(isset($_GET[" pg"]) && $ _GET["pg"]!=$this->pg){
  45. $this->url=str_replace("?pg=".$_GET["pg"],"?pg= $this->pg ",$this->url);
  46. $this->url=str_replace("&pg=".$_GET["pg"],"&pg=$this->pg", $this->url );
  47. }
  48. //12345 などの数値の形式でページネーションを生成します。
  49. if ($this->pagefor ($i=1;$ipage+1;$i++){
  50. $this->gt;thestr=$this-> ;thestr.Pager::makepg($i,$this->pg);
  51. }
  52. }else{
  53. if ($this->pgfor ($i=1;$i$this->thestr=$this-> thestr.Pager::makepg($i,$this->pg);
  54. }
  55. }else{
  56. if (6+$this->pg<=$this->page){
  57. for ($i=$this->pg-4;$i<$this-> pg+6;$i++){
  58. $this->thestr=$this->thestr.Pager::makepg($i,$this->pg);
  59. }
  60. }else{
  61. for ($i=$this->pg-4;$i<$this->page+1;$i++){
  62. $this->thestr=$this-> thestr.Pager::makepg($i,$this->pg);
  63. }

  64. }

  65. }
  66. }
  67. //生成上页下页等文字链接
  68. $this->backstr = Pager::gotoback($this->pg);
  69. $this->nextstr = Pager::gotonext($this->pg,$this->page);
  70. //echo (" 共".$this->countall." 条,每页".$this->countlist."条,共".$this->page."页".$this- >backstr.$this->thestr.$this->nextstr);
  71. }
  72. // 数字分を生成する補助関数
  73. function makepg($i,$pg){
  74. if ($i==$pg){
  75. return " ".$i."";
  76. }else{
  77. return " < ;u>".$i."";
  78. }
  79. }
  80. // 上一页等情報の関数を生成
  81. function gotoback($pg){
  82. if ($pg-1>0){
  83. return $this->gotoback=" 首页 上一页";
  84. }else{
  85. return $this->gotoback="首页 上一页 ";
  86. }
  87. }
  88. // 次の一页等情報の関数を生成
  89. function gotonext($pg,$page){
  90. if ($pg < $page){
  91. return " 下一页 尾页";
  92. }else{
  93. return " 下一页 尾页";
  94. }
  95. } bbs.it-home.org
  96. //処理url中$pg的メソッド、自動生成用pg=x
  97. function replacepg($url,$flag,$i){
  98. if ($flag == 1){
  99. $temp_pg = $this->pg;
  100. return str_replace("pg=".$temp_pg,"pg=".($this->pg+1),$url);
  101. }else if($flag == 2) {
  102. $temp_pg = $this->pg;
  103. return str_replace("pg=".$temp_pg,"pg=".($this->pg-1),$url);
  104. }else if($flag == 3) {
  105. $temp_pg = $this->pg;
  106. return str_replace("pg=".$temp_pg,"pg=1",$url);
  107. }else if($flag == 4){
  108. $temp_pg = $this->pg;
  109. return str_replace("pg=".$temp_pg,"pg=".$this->page,$url);
  110. }else if($flag == 5){
  111. $temp_pg = $this->pg;
  112. return str_replace("pg=".$temp_pg,"pg=".$i,$url);
  113. }else{
  114. $url を返す;
  115. }
  116. }
  117. //現在のURLを取得する方法
  118. function getUrl(){
  119. $url="http://".$_SERVER["HTTP_HOST"];
  120. if(isset($_SERVER["REQUEST_URI"])){
  121. $url.=$_SERVER["REQUEST_URI"];
  122. }else{
  123. $url.=$_SERVER["PHP_SELF"];
  124. if(!empty($_SERVER["QUERY_STRING"])){
  125. $url.="?".$_SERVER["QUERY_STRING"];
  126. }
  127. }
  128. //現在のURL里に追加pg=x字样
  129. if (!ereg("(pg=|PG=|pG=|Pg=)", $url)){
  130. if (!strpos( $url,"?")){
  131. $url = $url."?pg=1";
  132. }else{
  133. $url = $url."&pg=1";
  134. }
  135. }
  136. $url を返す;
  137. }
  138. }
  139. ?>

复制代


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。