Maison > développement back-end > tutoriel php > php分页类代码,可自定义参数php分页代码

php分页类代码,可自定义参数php分页代码

WBOY
Libérer: 2016-07-25 08:52:34
original
883 Les gens l'ont consulté
  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 = ""; //前后分页链接字符串
  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. /* 变量定义部分 end */
  25. /* 函数定义(类方法) begin */

  26. function __construct() {include_once 'DBHelper/DBHelper.php';//这是数据库操作类

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

  30. * 设置分页信息 begin
  31. * @param $db_table 表
  32. * @param $db_table_field 字段
  33. * @param $condition 條件
  34. * @param $sort 排序
  35. * @param $page_size 顯示條數
  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; //字段数组,
  43. $this->condition = $condition; //排序条件
  44. $this->sort = $sort; //排序条件
  45. //将要显示的字段名称
  46. //写入该数组
  47. /* db参数设置 end */
  48. /* 分页参数设置 begin */

  49. $this->page_size = $page_size;//每页显示记录的数目
  50. $this->link_num = $link_num;//显示翻页链接的数目
  51. $this->linkUrl = $url;
  52. $this->urlPara = $para;
  53. /* 分页参数设置 end */
  54. }
  55. /* 设置分页信息 end */

  56. /* 获取分页链接数据 begin */

  57. public function get() {

  58. $page_data[0] = $this->pagestring;
  59. $page_data[1] = $this->page_data;
  60. return $page_data;
  61. }
  62. /* 获取分页链接数据 end */

  63. /* 页码处理 begin */

  64. private function set_page() {

  65. if (isset($_REQUEST["page"])) {
  66. $this->page = intval($_REQUEST["page"]);
  67. } else {
  68. $this->page = 1;
  69. }
  70. }
  71. /* 页码处理 end */

  72. /* 获取db中记录的数目 begin */

  73. private function get_records() {

  74. $this->records = $this->dbHelper->counts($this->db_table, $this->condition);
  75. }
  76. /* 获取db中记录的数目 end */

  77. /* 建立翻页链接字符串 begin */

  78. private function page_link() {

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

  154. $this->pagestring.=$countStr." ".$currStr;
  155. if($this->page_count > 1){
  156. $this->pagestring.=$beginLink;
  157. if($this->page > 1){
  158. $this->pagestring.=$preLink;
  159. }
  160. $this->pagestring.=$currPage;
  161. if($this->page page_count){
  162. $this->pagestring.=$nextLink;
  163. }
  164. $this->pagestring.=$endLink.$jumpPage;
  165. }
  166. }
  167. /* 建立翻页链接字符串 end */

  168. /* 获取数据 begin */

  169. private function fetch_data() {

  170. if ($this->records) {
  171. $limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
  172. $this->page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
  173. }
  174. }
  175. /* 获取数据 end */

  176. /* 建立分页 begin */

  177. public function create_page() {

  178. $this->set_page();
  179. $this->get_records();
  180. $this->page_count = ceil($this->records / $this->page_size);
  181. $this->page_link();
  182. $this->fetch_data();
  183. }
  184. /* 建立分页 end */

  185. function __destruct() {

  186. }

  187. /* 函数定义(类方法) end */

  188. }
  189. /*
  190. 调用方法
  191. include_once 'PageLink.php';
  192. $pageLink = new PageLink(); //实例化对象
  193. $pageLink->set("table", "*(或字段)", "条件", "排序", "数据条数", "每页链接数", "页面(list.php)", "除?page=1外的其他参数(&id=1&name=test)");//传入参数
  194. $pageLink->create_page();//创建分页
  195. $list = $pageLink->get();//获得数据
  196. echo $list[0];//分页链接
  197. print_r($list[1]); //打印出数据,或按你的需要循环出来
  198. */
复制代码


source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal