欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-04-24 15:01:25
...
  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 .= "linkUrl
  121. ."?page='+document.getElementById('page_text_".$this->linkUrlNum."').value+'".$this->urlPara
  122. ."'\" name='page_submit' value='GO' />";
  123. $this->linkUrlNum++;
  124. if(!empty($this->linkFormat)){
  125. $tempLinkFormat = $this->linkFormat;
  126. $tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
  127. $tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
  128. if($this->page_count > 1){
  129. $tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
  130. if($this->page > 1){
  131. $tempLinkFormat = str_replace("上一頁", $preLink, $tempLinkFormat);
  132. }
  133. else{
  134. $tempLinkFormat = str_replace("上一頁", $noPreLink, $tempLinkFormat);
  135. }
  136. if($this->page page_count){
  137. $tempLinkFormat = str_replace("下一頁", $nextLink, $tempLinkFormat);
  138. }
  139. else{
  140. $tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
  141. }
  142. $tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
  143. $tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
  144. $tempLinkFormat = str_replace("跳轉", $jumpPage, $tempLinkFormat);
  145. }
  146. else{
  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. $tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
  153. }
  154. }

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

  169. /* 获取数据 begin */

  170. private function fetch_data() {

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

  177. /* 建立分页 begin */

  178. public function create_page() {

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

  186. function __destruct() {

  187. }

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

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