简体   繁体   English

分页:逻辑?

[英]Pagination: the logic?

Im making a pagination system that will paginate articles in my website.我正在制作一个分页系统,可以对我网站上的文章进行分页。

GOAL: Paginate an array of files (7 element/page)目标:对文件数组进行分页(7 个元素/页)

I got across a problem that ive been troubleshooting for 5+ hours... Heres the logic side of things, correct me if im wrong.我遇到了一个问题,我已经解决了 5 个多小时的问题......这是事情的逻辑方面,如果我错了,请纠正我。

Okay.好的。 So ive got 26 dummy articles (the alphabet) inside a folder.所以我在一个文件夹中得到了 26 篇虚拟文章(字母表)。 Lets find the number of files in there... I will call the result: variable X.让我们找到那里的文件数...我将调用结果:变量 X。

To get the number of pagination pages, im doing the following: X divided by 7. Obviously, this can output floats instead of integers.. So ill be rounding up the result using "cint"- which will always round upwards.要获得分页页数,我执行以下操作:X 除以 7。显然,这可以输出浮点数而不是整数。所以我将使用“cint”对结果进行四舍五入 - 它将始终向上舍入。 Lets call the number of pages "Z".让我们称页数为“Z”。

So me and my new friend Z want to tell some kind of function to fetch those articles.所以我和我的新朋友 Z 想告诉某种函数来获取这些文章。 Ive made the following equations to find the start and the end of what articles I want to show.我已经做了以下等式来找到我想要展示的文章的开始和结束。

$start = Z * 7 - 7 $start = Z * 7 - 7

$end = Z * 7 $end = Z * 7

Those equations generate这些方程产生

0 to 7 for page 1. Expected result (not reality):第 1 页的 0 到 7。预期结果(不是现实):

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

8 to 15 for page 2. Expected result (not reality).第 2 页的 8 到 15。预期结果(不是现实)。

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

And so on...等等...

So, using my superior brain (sike) I managed to generate the following output for page 1:因此,使用我的高级大脑(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

But, wierdly enough, when I go to page 2, I get... this mess.但是,奇怪的是,当我翻到第 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

...And when I go to page 3, some of the results from page 2 appears! ...当我转到第 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

Finally, I get one last page (page 4) with the final last result from page 3.最后,我得到了最后一页(第 4 页)和第 3 页的最后结果。

Heres the code...这是代码...

 <?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>"; } } ?>

I think this problem is caused by my awful logic skills.我认为这个问题是由我糟糕的逻辑能力造成的。 Could anyone correct me, point me to docs?谁能纠正我,给我指点文档?

Thanks alot for yall time :)非常感谢你们的时间:)

array_slice doesn't expect the first and the last index, but the first index and the length (number of elements to extract). array_slice 不需要第一个和最后一个索引,而是第一个索引和长度(要提取的元素数)。 So you should put:所以你应该把:

array_slice($post_array, $start, 7); array_slice($post_array, $start, 7);

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

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