搜索
首页后端开发php教程一个php分页类代码(附效果图)

  1. /**

  2. * 简单分页类
  3. * Page.class.php
  4. */
  5. class Page
  6. {
  7. private $page_num; //每页显示的信息条数
  8. private $page_all_no; //信息的总条数
  9. private $page_len; //显示多少个页码
  10. private $page; //当前的页数
  11. private $page_max; //页数最大值
  12. private $page_no_array; //页数的数组
  13. public $start_num; //查询语句limit的起始值
  14. private $page_change; //在第几个页码开始 页码递增
  15. private $URL; //获取当前页面的URL
  16. public function __construct($page_all_no, $page_num=5, $page_len=5)

  17. {
  18. $this->page_all_no = intval($page_all_no);
  19. $this->page_num = intval($page_num);
  20. $this->page_len = intval($page_len);
  21. $this->URL = $_SERVER['REQUEST_URI'];
  22. $this->max_page(); //得到页数的最大值
  23. $this->page_no(); //得到当前页数
  24. $this->page_no_array(); //页数数组
  25. $this->start_num(); //得到sql语句中limit的起始值
  26. $this->change_page(); //得到递增开始的页码$this->page_change
  27. $this->getURL(); //得到当前的URL并处理返回
  28. }
  29. private function isArray($str,$str_self)

  30. {
  31. if(!is_array($str)) throw new Exception("$str_self must be an Array type");
  32. }
  33. private function page_no()

  34. {
  35. $this->page = isset($_GET['page']) ? $_GET['page'] : 1;
  36. if(isset($_GET['page']) && $_GET['page'] page = 1;
  37. if(isset($_GET['page']) && $_GET['page'] > $this->page_max) $this->page = $this->page_max;
  38. return $this->page;
  39. }
  40. private function max_page()

  41. {
  42. return $this->page_max = $this->page_all_no page_all_no/$this->page_num);
  43. }
  44. private function change_page()

  45. {
  46. return $this->page_change = ceil($this->page_len / 2);
  47. }
  48. private function page_no_array()

  49. {
  50. return $this->page_no_array = range(1, $this->page_max);
  51. }
  52. private function start_num()

  53. {
  54. return $this->start_num = $this->page_num * ($this->page - 1);
  55. }
  56. private function getURL()

  57. {
  58. if(!empty($_SERVER['argc']) ? $_SERVER['argc'] == 0 : strpos($_SERVER['REQUEST_URI'], '?') === false)
  59. {
  60. $this->URL = $this->URL.'?';
  61. }else{
  62. $url_a = "/\?page=[0-9]{1,}/";
  63. $url_b = "/&page=[0-9]{1,}/";
  64. ereg("\?page=[0-9]{1,}", $this->URL) ? $this->URL = preg_replace($url_a, "?", $this->URL):
  65. ereg("&page=[0-9]{1,}", $this->URL) ? $this->URL = preg_replace($url_b, "&", $this->URL):$this->URL = $this->URL.'&';
  66. }
  67. return $this->URL;
  68. }
  69. private function header_info($total_data_modifier='总数:', $total_page_modifier=' 总页数:', $current_page_modifier=' 当前页:', $header_info_modifier='', $header_data_color = 'red'){

  70. if($this->page_max != 1 && $this->page_all_no != 0)
  71. {
  72. $header_info = $total_data_modifier.''.$this->page_all_no.'';
  73. $header_info .= $total_page_modifier.''.$this->page_max.'';
  74. $header_info .= $current_page_modifier.''.$this->page.' '."\r\n";
  75. if(!empty($header_info_modifier)) $header_info = $header_info_modifier.$header_info;
  76. return $header_info;
  77. }else{
  78. return NULL;
  79. }
  80. }
  81. private function first($first_format = '第一页')

  82. {
  83. return $this->page == 1 ? $first_format."\r\n" : ''.$first_format.''."\r\n";
  84. }
  85. private function last($last_format = '上一页')

  86. {
  87. if($this->page == 1) return $last_format."\r\n";
  88. return $this->page-1 == 1 ? ''.$last_format.''."\r\n" : ''.$last_format.''."\r\n";
  89. }
  90. private function page_num_format($separator=' ', $left_modifier='[', $right_modifier=']', $both_sides=false, $current_page_color = 'red')

  91. {
  92. empty($separator) ? $separator = ' ' : $separator;
  93. $page_array = '';
  94. if($this->page_max page_len)
  95. {
  96. for ($i=0; $i page_max; $i++)
  97. {
  98. if($this->page_no_array[$i] == $this->page)
  99. {
  100. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."\r\n";
  101. }else{
  102. if($i == 0)
  103. {
  104. $page_one = substr($this->URL,0,strlen($this->URL)-1);
  105. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."\r\n";
  106. }else{
  107. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."\r\n";
  108. }
  109. }
  110. $page_array .= $the[$i].$separator;
  111. }
  112. if($both_sides === false)
  113. {
  114. $page_array = substr($page_array,0,strrpos($page_array, $separator));
  115. }elseif ($both_sides === true){
  116. $page_array = $separator.$page_array;
  117. }else{
  118. throw new Exception('ERROR: $both_sides must be a boolean type.');
  119. }
  120. }else{
  121. if($this->page page_change)
  122. {
  123. $i_start = 0;
  124. }else{
  125. $i_start = $this->page - $this->page_change;
  126. //如果最大的页码已显示,那么开始页就不会在递增
  127. if($i_start >= $this->page_max - $this->page_len){
  128. $i_start = $this->page_max - $this->page_len;
  129. }
  130. }
  131. $i_end = ($i_start+$this->page_len) - 1;
  132. for ($i = $i_start; $i {
  133. if($this->page_no_array[$i] == $this->page)
  134. {
  135. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."\r\n";
  136. }else{
  137. if($i == 0)
  138. {
  139. $page_one = substr($this->URL,0,strlen($this->URL)-1);
  140. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."\r\n";
  141. }else{
  142. $the[$i] = ''.$left_modifier.$this->page_no_array[$i].$right_modifier.''."\r\n";
  143. }
  144. }
  145. $page_array .= $the[$i].$separator;
  146. }
  147. if($both_sides === false)
  148. {
  149. $page_array = substr($page_array,0,strrpos($page_array, $separator));
  150. }elseif ($both_sides === true){
  151. $page_array = $separator.$page_array;
  152. }else{
  153. throw new Exception('ERROR: $both_sides must be a boolean type.');
  154. }
  155. }
  156. return $page_array;
  157. }
  158. private function next($next_format = '下一页')

  159. {
  160. if($this->page >= $this->page_max) return $next_format."\r\n";
  161. return ''.$next_format.''."\r\n";
  162. }
  163. private function end($end_format = '最后一页')

  164. {
  165. return $this->page >= $this->page_max ? $end_format."\r\n" : ''.$end_format.''."\r\n";
  166. }
  167. public function select_page($target='self', $select_page_mode='PMA', $value_left_modifier='', $value_right_modifier='')

  168. {
  169. if($this->page_max != 1 && $this->page_all_no != 0)
  170. {
  171. if($select_page_mode === 'NORMAL')
  172. {
  173. $page_no_array = $this->page_no_array;
  174. }elseif($select_page_mode === 'PMA'){
  175. $page_no_array = $this->PMA_page_no_array();
  176. }else{
  177. throw new Exception('$select_page_mode is unknown mode');
  178. }
  179. $select_page = '';
  180. $select_page .= ''. "\n";
  181. return $select_page;
  182. }else{
  183. return NULL;
  184. }
  185. }
  186. private function PMA_page_no_array()

  187. {
  188. $showAll = 200;
  189. $sliceStart = 5;
  190. $sliceEnd = 5;
  191. $percent = 20;
  192. $range = 10;
  193. if ($this->page_max $this->PMA_page_no_array = range(1, $this->page_max);

  194. } else {
  195. $this->PMA_page_no_array = array();
  196. for ($i = 1; $i $this->PMA_page_no_array[] = $i;
  197. }
  198. for ($i = $this->page_max - $sliceEnd; $i page_max; $i++) {
  199. $this->PMA_page_no_array[] = $i;
  200. }
  201. $i = $sliceStart;
  202. $x = $this->page_max - $sliceEnd;
  203. $met_boundary = false;
  204. while ($i if ($i >= ($this->page - $range) && $i page + $range)) {
  205. $i++;
  206. $met_boundary = true;
  207. } else {
  208. $i = $i + floor($this->page_max / $percent);
  209. if ($i > ($this->page - $range) && !$met_boundary) {
  210. $i = $this->page - $range;
  211. }
  212. }
  213. if ($i > 0 && $i $this->PMA_page_no_array[] = $i;
  214. }
  215. }
  216. sort($this->PMA_page_no_array);
  217. $this->PMA_page_no_array = array_unique($this->PMA_page_no_array);
  218. }
  219. return $this->PMA_page_no_array;
  220. }
  221. public function key_change_page(){

  222. echo '';
  223. }
  224. public function eshow()

  225. {
  226. return $this->last().$this->next();
  227. }
  228. public function showForHelp()

  229. {
  230. return $this->first('首页', '').$this->last('上一页', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一页', '').$this->end('尾页', '');
  231. }
  232. public function show()

  233. {
  234. return $this->header_info().$this->first('首页', '').$this->last('上一页', '').$this->page_num_format('', '', '', false, '#FF8500').$this->next('下一页', '').$this->end('尾页', '');
  235. }
  236. }
复制代码

2,php分页类的使用方法:

  1. header('content-type:text/html;charset=utf-8');
  2. include('Page.class.php');
  3. $page_all_no = 12000; //数据的总条数
  4. $page_num = 25; //设置每页显示的条数
  5. $page_len = 7; //最多显示的页码数
  6. $page = new Page($page_all_no, $page_num, $page_len);
  7. $start_num = $page->start_num;
  8. $sql = "select * from table limit {$start_num}, {$page_num}";
  9. $page->key_change_page(); //方向键翻页
  10. var_dump($sql);
  11. //自带三种分页形式,可再按需要添加新方法,像下面那样调用
  12. echo '

    '.$page->select_page('self', 'NORMAL').'

    '; //这里的第二个参数默认为PMA模式,两种模式差别在上面已给出
  13. echo '

    '.$page->eshow().'

    ';
  14. echo '

    '.$page->show().'

    ';
复制代码

3,php分页类,效果图: php分页类代码



声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP记录:PHP日志分析的最佳实践PHP记录:PHP日志分析的最佳实践Mar 10, 2025 pm 02:32 PM

PHP日志记录对于监视和调试Web应用程序以及捕获关键事件,错误和运行时行为至关重要。它为系统性能提供了宝贵的见解,有助于识别问题并支持更快的故障排除

在Laravel中使用Flash会话数据在Laravel中使用Flash会话数据Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

php中的卷曲:如何在REST API中使用PHP卷曲扩展php中的卷曲:如何在REST API中使用PHP卷曲扩展Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

简化的HTTP响应在Laravel测试中模拟了简化的HTTP响应在Laravel测试中模拟了Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

在Codecanyon上的12个最佳PHP聊天脚本在Codecanyon上的12个最佳PHP聊天脚本Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

解释PHP中晚期静态结合的概念。解释PHP中晚期静态结合的概念。Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

自定义/扩展框架:如何添加自定义功能。自定义/扩展框架:如何添加自定义功能。Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

禅工作室 13.0.1

禅工作室 13.0.1

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