ホームページ  >  記事  >  バックエンド開発  >  PHP ページング クラス コード、カスタマイズ可能なパラメータ PHP ページング コード

PHP ページング クラス コード、カスタマイズ可能なパラメータ PHP ページング コード

WBOY
WBOYオリジナル
2016-07-25 08:52:34817ブラウズ
  1. class PageLink {

  2. /* ページング表示パラメータ設定*/
  3. private $db_table = "";
  4. private $db_table_field = "";//表示されるデータベース内のフィールド
  5. private $ condition = ""; //クエリ条件
  6. private $sort = "" //ソート条件
  7. private $page_size = 0 //各ページに表示されるレコード数
  8. private $link_num = 0;ページ番号リンク
  9. private $page = 1; //現在のページ番号
  10. private $records = 0; //テーブル内の総レコード数
  11. private $page_count = 0; //総ページ数
  12. private $pagestring = " "; //前後のリンク文字をページングする String
  13. private $linkUrl = ""; //現在のページのパス
  14. private $urlPara = ""; //現在のページの URL パラメーター
  15. private $linkUrlNum = 1;
  16. /*
  17. *カスタムページング
  18. */
  19. private $linkFormat = "";

  20. /* 取得データ*/

  21. private $page_data = "";//データベースから取得したデータ、

  22. // $page_data は受信した二次元配列です

  23. private $dbHelper;

  24. /* 変数定義部終了 */

  25. /*関数定義(クラスメソッド) begin * /

  26. function __construct() {include_once 'DBHelper/DBHelper.php';//これはデータベース操作クラスです

  27. $this->dbHelper = new DBHelper();
  28. }
  29. /*

  30. * ページング情報の設定開始
  31. * @param $db_table テーブル
  32. * @param $db_table_field フィールド
  33. * @param $condition 条件
  34. * @param $ sort ソート
  35. * @param $page_size display 項目数
  36. * @param $link_num 数値リンク数
  37. * @param $url ページパス
  38. * @param $para URL パラメータ
  39. */
  40. public function set($db_table, $ db_table_field, $condition, $sort, $page_size, $link_num, $url, $para) {
  41. $this->db_table = $db_table;//テーブル名
  42. $this->db_table_field = $db_table_field; array,
  43. $this->condition = $condition; //ソート条件
  44. $this->sort = $sort; //表示するフィールド名
  45. //配列に書き込む* db パラメータ設定終了 */

  46. /* ページングパラメータ設定開始 */

  47. $this->page_size = $page_size;//各ページに表示されるレコード数
  48. $this- >link_num = $link_num;//ページめくりリンク数を表示
  49. $this->linkUrl = $url;
  50. $this->urlPara = $para;
  51. /* ページめくりパラメータの設定 end */
  52. }
  53. /* ページング情報の設定end */

  54. /* ページングリンクデータ取得 begin */

  55. public function get() {

  56. $page_data[0] = $this->pagestring ;
  57. $page_data[1] = $this->page_data;
  58. return $page_data;
  59. }

  60. /* ページネーション リンク データを取得するend */

  61. private function set_page() {

  62. if (isset($_REQUEST["page"]) ) {
  63. $this->page = intval($ _REQUEST["page"]);
  64. } else {
  65. $this->page = 1;
  66. }
  67. }

  68. / * ページ番号処理終了 */

  69. < ;p>/* データベース内のレコード数の取得 begin */

  70. private function get_records() {

  71. $this-> ;records = $this->dbHelper->counts($ this->db_table, $this->condition);
  72. }

  73. /* db 内のレコード数を取得しますend */

  74. /* ページめくりリンク文字列の作成 begin */

  75. private function page_link() {

  76. $checkPage = intval($this->ページ / $this->link_num);
  77. $startPage = 1 ;
  78. $stopPage = 1;
  79. if($checkPage == 0 && $this->page < $this->link_num){
  80. $startPage = 1;
  81. }
  82. $linkPage = ($this->gt; link_num / 2);
  83. if($this->page > $linkPage){
  84. $startPage = $this->page - $linkPage;
  85. }
  86. if(($startPage+$this->link_num)> ;$this->page_count){
  87. $startPage = $this->page_count - $this->link_num + 1;
  88. }
  89. if($ startPage$startPage = 1;
  90. }
  91. $ stopPage = $startPage+($this->link_num - 1);
  92. if($stopPage> $this->page_count){
  93. $stopPage = $this ->ページ数;
  94. }
  95. $countStr = "共".$this->records."條記錄";
  96. $currStr = "".$this->page."/".$this- >page_count."ページ ";
  97. $beginLink = "首ページ< ;/a>";
  98. $preLink = "上の一ページ";
  99. $nextLink = "下のページ";
  100. $noPreLink = "上のページ";
  101. $noNextLink = "次のページ";
  102. $endLink = "尾ページ";
  103. if($this->page > ($linkPage + 1) && $this->page_count > $this->link_num){
  104. $currPage .= "1";
  105. }
  106. for($i =$startPage;$iif($i == $this->page){
  107. $currPage .= "".$i ."";
  108. }
  109. else{
  110. $currPage .= "".$i."";
  111. }
  112. }
  113. if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
  114. $currPage .= "...".$this->page_count."";
  115. }
  116. $jumpPage .= " ";
  117. $jumpPage .= "linkUrl
  118. ."?page='+document.getElementById('page_text_".$this->linkUrlNum."')。 value+'".$this->urlPara
  119. ."'" name='page_submit' value='GO' />";
  120. $this->linkUrlNum++;
  121. if(!empty($this->linkFormat )){
  122. $tempLinkFormat = $this->linkFormat;
  123. $tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
  124. $tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
  125. if($this->page_count > 1){
  126. $tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
  127. if($this->page > 1){
  128. $tempLinkFormat = str_replace ("上一ページ", $preLink, $tempLinkFormat);
  129. }
  130. else{
  131. $tempLinkFormat = str_replace("上一ページ", $noPreLink, $tempLinkFormat);
  132. }
  133. if($this->page < ; $this->page_count){
  134. $tempLinkFormat = str_replace("次の一頁", $nextLink, $tempLinkFormat);
  135. }
  136. else{
  137. $tempLinkFormat = str_replace("次の一頁", $noNextLink, $tempLinkFormat );
  138. }
  139. $tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
  140. $tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
  141. $tempLinkFormat = str_replace("跳轉", $ JumpPage, $tempLinkFormat);
  142. }
  143. else{
  144. $tempLinkFormat = str_replace("首頁", "", $tempLinkFormat);
  145. $tempLinkFormat = str_replace("上の一頁", "", $tempLinkFormat);
  146. $tempLinkFormat = str_replace("次の一頁", "", $tempLinkFormat);
  147. $tempLinkFormat = str_replace("尾頁", "", $tempLinkFormat);
  148. $tempLinkFormat = str_replace("分頁", "", $tempLinkFormat) ;
  149. $tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
  150. }

  151. }

  152. $this->pagestring.=$countStr." ".$currStr;
  153. if($this->ページ数> 1){
  154. $this->pagestring.=$beginLink;
  155. if($this->page> 1){
  156. $this->pagestring.=$preLink;
  157. }
  158. $this->pagestring .=$currPage;
  159. if($this->page < $this->page_count){
  160. $this->pagestring.=$nextLink;
  161. }
  162. $this->pagestring.=$endLink. $jumpPage;
  163. }
  164. }

  165. /* ページめくりリンク文字列の作成 end */

  166. /* データの取得 begin */

  167. private function fetch_data () {

  168. if ($this->records) {
  169. $limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
  170. $this - >page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
  171. }
  172. }

    /* データ取得 end */

  173. /* ページング作成 begin */

  174. public function create_page() {

  175. $this-> ; set_page();
  176. $this->get_records();
  177. $this->page_count = ceil($this->records / $this->page_size);
  178. $this->page_link();
  179. $this->fetch_data();
  180. }

  181. /* ページネーションの作成 end */

  182. function __destruct() {

  183. < ;p>}

  184. /* 関数定義(クラスメソッド) end */

  185. }
  186. /*
  187. メソッド呼び出し
  188. include_once 'PageLink.php';
  189. $pageLink = new PageLink() ; //インスタントオブジェクト
  190. $pageLink->set("テーブル", "*(またはフィールド)", "条件", "ソート", "データ項目数", "ページごとのリンク数", "page( list.php)", "その他のパラメータ (?page=1 (&id=1&name=test) を除く)");//パラメータを渡します
  191. $pageLink->create_page();// ページングを作成します
  192. $list = $ pageLink- >get();//データを取得します
  193. echo $list[0];//ページリンク
  194. print_r($list[1]); //データを出力するか、必要に応じてループアウトします
  195. */ < ;/p>
コードをコピー

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