Home > Backend Development > PHP Tutorial > PHP 分页的计算

PHP 分页的计算

WBOY
Release: 2016-06-23 13:01:04
Original
2062 people have browsed it

$maxpage;  //最大页数$page;         //当前页数$pageset     //显示个数
Copy after login

$pageset = 10 显示10个可以点击的按钮()如下


$pageshow //页数范围

假设:
$maxpage = 20;
$page = 7
$pageset = 10;

$pageshow = array(2,3,4,5,6,7,8,9,10,11);


$pageshow 要怎么计算出来?


回复讨论(解决方案)

中间$page-5到$page+4
前后靠近页头页尾的部分再做一下判定就出来了

测试

$page = 7;$maxpage = 20;$pageset = 10;for($page=1; $page<=20; $page++) {  $s = 1;  if($page > $pageset / 2) $s = $page - $pageset / 2;  if($maxpage - $page < $pageset / 2) $s = $maxpage - $pageset + 1;  $pageshow = range($s, $s + $pageset - 1);  printf("%d : %s\n", $page, join(' ', $pageshow));}
Copy after login
Copy after login
1 : 1 2 3 4 5 6 7 8 9 102 : 1 2 3 4 5 6 7 8 9 103 : 1 2 3 4 5 6 7 8 9 104 : 1 2 3 4 5 6 7 8 9 105 : 1 2 3 4 5 6 7 8 9 106 : 1 2 3 4 5 6 7 8 9 107 : 2 3 4 5 6 7 8 9 10 118 : 3 4 5 6 7 8 9 10 11 129 : 4 5 6 7 8 9 10 11 12 1310 : 5 6 7 8 9 10 11 12 13 1411 : 6 7 8 9 10 11 12 13 14 1512 : 7 8 9 10 11 12 13 14 15 1613 : 8 9 10 11 12 13 14 15 16 1714 : 9 10 11 12 13 14 15 16 17 1815 : 10 11 12 13 14 15 16 17 18 1916 : 11 12 13 14 15 16 17 18 19 2017 : 11 12 13 14 15 16 17 18 19 2018 : 11 12 13 14 15 16 17 18 19 2019 : 11 12 13 14 15 16 17 18 19 2020 : 11 12 13 14 15 16 17 18 19 20
Copy after login
Copy after login

测试

$page = 7;$maxpage = 20;$pageset = 10;for($page=1; $page<=20; $page++) {  $s = 1;  if($page > $pageset / 2) $s = $page - $pageset / 2;  if($maxpage - $page < $pageset / 2) $s = $maxpage - $pageset + 1;  $pageshow = range($s, $s + $pageset - 1);  printf("%d : %s\n", $page, join(' ', $pageshow));}
Copy after login
Copy after login
1 : 1 2 3 4 5 6 7 8 9 102 : 1 2 3 4 5 6 7 8 9 103 : 1 2 3 4 5 6 7 8 9 104 : 1 2 3 4 5 6 7 8 9 105 : 1 2 3 4 5 6 7 8 9 106 : 1 2 3 4 5 6 7 8 9 107 : 2 3 4 5 6 7 8 9 10 118 : 3 4 5 6 7 8 9 10 11 129 : 4 5 6 7 8 9 10 11 12 1310 : 5 6 7 8 9 10 11 12 13 1411 : 6 7 8 9 10 11 12 13 14 1512 : 7 8 9 10 11 12 13 14 15 1613 : 8 9 10 11 12 13 14 15 16 1714 : 9 10 11 12 13 14 15 16 17 1815 : 10 11 12 13 14 15 16 17 18 1916 : 11 12 13 14 15 16 17 18 19 2017 : 11 12 13 14 15 16 17 18 19 2018 : 11 12 13 14 15 16 17 18 19 2019 : 11 12 13 14 15 16 17 18 19 2020 : 11 12 13 14 15 16 17 18 19 20
Copy after login
Copy after login


$pageset = 单数的时候会出错;
$pageset 是可变的

取下整就是了,算式中并没有使用常量

$maxpage = 20;$pageset = 7;for($page=1; $page<=20; $page++) {  $s = 1;  if($page > $pageset / 2) $s = $page - floor($pageset / 2);  if($maxpage - $page < $pageset / 2) $s = $maxpage - $pageset + 1;  $pageshow = range($s, $s + $pageset - 1);  printf("%d : %s\n", $page, join(' ', $pageshow));}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template