当前位置:Gxlcms > PHP教程 > php分页类代码,可自定义参数php分页代码

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

时间:2021-07-01 10:21:17 帮助过:37人阅读

  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 < $this->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<1){
  93. $startPage = 1;
  94. }
  95. $stopPage = $startPage+($this->link_num - 1);
  96. if($stopPage> $this->page_count){
  97. $stopPage = $this->page_count;
  98. }
  99. $countStr = "共".$this->records."條記錄";
  100. $currStr = "".$this->page."/".$this->page_count."頁 ";
  101. $beginLink = "linkUrl."?page=1".$this->urlPara."'>首頁";
  102. $preLink = "linkUrl."?page=".($this->page - 1).$this->urlPara."'>上一頁";
  103. $nextLink = "linkUrl."?page=".($this->page + 1).$this->urlPara."'>下一頁";
  104. $noPreLink = "上一頁";
  105. $noNextLink = "下一頁";
  106. $endLink = "linkUrl."?page=".$this->page_count.$this->urlPara."'>尾頁";
  107. if($this->page > ($linkPage + 1) && $this->page_count > $this->link_num){
  108. $currPage .= "linkUrl."?page=1".$this->urlPara."'>1";
  109. }
  110. for($i=$startPage;$i<=$stopPage;$i++){
  111. if($i == $this->page){
  112. $currPage .= "".$i."";
  113. }
  114. else{
  115. $currPage .= "linkUrl."?page=".$i.$this->urlPara."'>".$i."";
  116. }
  117. }
  118. if(($this->page_count - $this->page) > $linkPage && $this->page_count > $this->link_num){
  119. $currPage .= "....$this->linkUrl."?page=".$this->page_count.$this->urlPara."'>".$this->page_count."";
  120. }
  121. $jumpPage .= " ";
  122. $jumpPage .= "linkUrl
  123. ."?page='+document.getElementById('page_text_".$this->linkUrlNum."').value+'".$this->urlPara
  124. ."'\" name='page_submit' value='GO' />";
  125. $this->linkUrlNum++;
  126. if(!empty($this->linkFormat)){
  127. $tempLinkFormat = $this->linkFormat;
  128. $tempLinkFormat = str_replace("總記錄", $countStr, $tempLinkFormat);
  129. $tempLinkFormat = str_replace("頁次", $currStr, $tempLinkFormat);
  130. if($this->page_count > 1){
  131. $tempLinkFormat = str_replace("首頁", $beginLink, $tempLinkFormat);
  132. if($this->page > 1){
  133. $tempLinkFormat = str_replace("上一頁", $preLink, $tempLinkFormat);
  134. }
  135. else{
  136. $tempLinkFormat = str_replace("上一頁", $noPreLink, $tempLinkFormat);
  137. }
  138. if($this->page < $this->page_count){
  139. $tempLinkFormat = str_replace("下一頁", $nextLink, $tempLinkFormat);
  140. }
  141. else{
  142. $tempLinkFormat = str_replace("下一頁", $noNextLink, $tempLinkFormat);
  143. }
  144. $tempLinkFormat = str_replace("尾頁", $endLink, $tempLinkFormat);
  145. $tempLinkFormat = str_replace("分頁", $currPage, $tempLinkFormat);
  146. $tempLinkFormat = str_replace("跳轉", $jumpPage, $tempLinkFormat);
  147. }
  148. else{
  149. $tempLinkFormat = str_replace("首頁", "", $tempLinkFormat);
  150. $tempLinkFormat = str_replace("上一頁", "", $tempLinkFormat);
  151. $tempLinkFormat = str_replace("下一頁", "", $tempLinkFormat);
  152. $tempLinkFormat = str_replace("尾頁", "", $tempLinkFormat);
  153. $tempLinkFormat = str_replace("分頁", "", $tempLinkFormat);
  154. $tempLinkFormat = str_replace("跳轉", "", $tempLinkFormat);
  155. }

  156. }

  157. $this->pagestring.=$countStr." ".$currStr;
  158. if($this->page_count > 1){
  159. $this->pagestring.=$beginLink;
  160. if($this->page > 1){
  161. $this->pagestring.=$preLink;
  162. }
  163. $this->pagestring.=$currPage;
  164. if($this->page < $this->page_count){
  165. $this->pagestring.=$nextLink;
  166. }
  167. $this->pagestring.=$endLink.$jumpPage;
  168. }
  169. }

  170. /* 建立翻页链接字符串 end */

  171. /* 获取数据 begin */

  172. private function fetch_data() {

  173. if ($this->records) {
  174. $limit = ($this->page - 1) * $this->page_size . ",$this->page_size";
  175. $this->page_data = $this->dbHelper->fetch($this->db_table, $this->condition, $this->sort, $limit);
  176. }
  177. }

  178. /* 获取数据 end */

  179. /* 建立分页 begin */

  180. public function create_page() {

  181. $this->set_page();
  182. $this->get_records();
  183. $this->page_count = ceil($this->records / $this->page_size);
  184. $this->page_link();
  185. $this->fetch_data();
  186. }

  187. /* 建立分页 end */

  188. function __destruct() {

  189. }

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

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

人气教程排行