搜索
首页后端开发php教程php分页类代码,可自定义参数php分页代码

  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. */
复制代码


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
使用数据库存储会话的优点是什么?使用数据库存储会话的优点是什么?Apr 24, 2025 am 12:16 AM

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

您如何在PHP中实现自定义会话处理?您如何在PHP中实现自定义会话处理?Apr 24, 2025 am 12:16 AM

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

什么是会话ID?什么是会话ID?Apr 24, 2025 am 12:13 AM

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

您如何在无状态环境(例如API)中处理会议?您如何在无状态环境(例如API)中处理会议?Apr 24, 2025 am 12:12 AM

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

您如何防止与会议有关的跨站点脚本(XSS)攻击?您如何防止与会议有关的跨站点脚本(XSS)攻击?Apr 23, 2025 am 12:16 AM

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

您如何优化PHP会话性能?您如何优化PHP会话性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

什么是session.gc_maxlifetime配置设置?什么是session.gc_maxlifetime配置设置?Apr 23, 2025 am 12:10 AM

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

您如何在PHP中配置会话名?您如何在PHP中配置会话名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具