繁体   English   中英

分页:逻辑?

[英]Pagination: the logic?

我正在制作一个分页系统,可以对我网站上的文章进行分页。

目标:对文件数组进行分页(7 个元素/页)

我遇到了一个问题,我已经解决了 5 个多小时的问题......这是事情的逻辑方面,如果我错了,请纠正我。

好的。 所以我在一个文件夹中得到了 26 篇虚拟文章(字母表)。 让我们找到那里的文件数...我将调用结果:变量 X。

要获得分页页数,我执行以下操作:X 除以 7。显然,这可以输出浮点数而不是整数。所以我将使用“cint”对结果进行四舍五入 - 它将始终向上舍入。 让我们称页数为“Z”。

所以我和我的新朋友 Z 想告诉某种函数来获取这些文章。 我已经做了以下等式来找到我想要展示的文章的开始和结束。

$start = Z * 7 - 7

$end = Z * 7

这些方程产生

第 1 页的 0 到 7。预期结果(不是现实):

a、b、c、d、e、f、g。

第 2 页的 8 到 15。预期结果(不是现实)。

h、i、j、k、l、m、n。

等等...

因此,使用我的高级大脑(sike),我设法为第 1 页生成了以下输出:

 CHOOSE PAGE: 1 2 3 4 Youre at page 1 Theres 26 articles Showing 0 to 7 a - Thursday, 4th of April 2019 @ 20:54:02 b - Thursday, 4th of April 2019 @ 20:54:04 c - Thursday, 4th of April 2019 @ 20:54:08 d - Thursday, 4th of April 2019 @ 20:54:10 e - Thursday, 4th of April 2019 @ 20:54:13 f - Thursday, 4th of April 2019 @ 20:54:15 g - Thursday, 4th of April 2019 @ 20:54:18

但是,奇怪的是,当我翻到第 2 页时,我明白了……这个烂摊子。

 CHOOSE PAGE: 1 2 3 4 Youre at page 2 Theres 26 articles Showing 7 to 14 h - Thursday, 4th of April 2019 @ 20:54:22 i - Thursday, 4th of April 2019 @ 20:54:24 j - Thursday, 4th of April 2019 @ 20:54:28 k - Thursday, 4th of April 2019 @ 20:54:31 l - Thursday, 4th of April 2019 @ 20:54:34 m - Thursday, 4th of April 2019 @ 20:54:37 n - Thursday, 4th of April 2019 @ 20:54:39 o - Thursday, 4th of April 2019 @ 20:54:42 p - Thursday, 4th of April 2019 @ 20:54:44 q - Thursday, 4th of April 2019 @ 20:55:47 r - Thursday, 4th of April 2019 @ 20:55:49 s - Thursday, 4th of April 2019 @ 20:55:51 t - Thursday, 4th of April 2019 @ 20:55:53 u - Thursday, 4th of April 2019 @ 20:55:55

...当我转到第 3 页时,会出现第 2 页的一些结果!

 CHOOSE PAGE: 1 2 3 4 Youre at page 3 Theres 26 articles Showing 14 to 21 o - Thursday, 4th of April 2019 @ 20:54:42 p - Thursday, 4th of April 2019 @ 20:54:44 q - Thursday, 4th of April 2019 @ 20:55:47 r - Thursday, 4th of April 2019 @ 20:55:49 s - Thursday, 4th of April 2019 @ 20:55:51 t - Thursday, 4th of April 2019 @ 20:55:53 u - Thursday, 4th of April 2019 @ 20:55:55 v - Thursday, 4th of April 2019 @ 20:55:57 w - Thursday, 4th of April 2019 @ 20:56:00 x - Thursday, 4th of April 2019 @ 20:56:03 y - Thursday, 4th of April 2019 @ 20:56:05 z - Thursday, 4th of April 2019 @ 20:56:07

最后,我得到了最后一页(第 4 页)和第 3 页的最后结果。

这是代码...

 <?php $page = strip_tags($_GET['p']); if(empty($page)){$page = "1";} $post_array = glob("post/*"); $post_count = count($post_array); $page_num = ceil($post_count / 7); echo "CHOOSE PAGE: "; for($i = 1; $i<$page_num+1; $i++){ echo "<a href=\\"?p={$i}\\">{$i}</a> "; } if($page>$page_num){ echo "<br>error"; } elseif(!is_numeric($page)) { echo "<br>error"; } else {echo "<br>Youre at page {$page}<br>"; echo "Theres {$post_count} articles<br><br>"; $start = $page * 7 - 7; $end = $page * 7; $post_array_sliced = array_slice($post_array, $start, $end); echo "Showing {$start} to {$end}<br><br>"; foreach ($post_array_sliced as $post){ $post_name = pathinfo($post)['filename']; $post_date = filemtime($post); echo "{$post_name} - ".date('l, jS \\of FY @ H:i:s', $post_date)."<br>"; } } ?>

我认为这个问题是由我糟糕的逻辑能力造成的。 谁能纠正我,给我指点文档?

非常感谢你们的时间:)

array_slice 不需要第一个和最后一个索引,而是第一个索引和长度(要提取的元素数)。 所以你应该把:

array_slice($post_array, $start, 7);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM