简体   繁体   中英

Pagination: the logic?

Im making a pagination system that will paginate articles in my website.

GOAL: Paginate an array of files (7 element/page)

I got across a problem that ive been troubleshooting for 5+ hours... Heres the logic side of things, correct me if im wrong.

Okay. So ive got 26 dummy articles (the alphabet) inside a folder. Lets find the number of files in there... I will call the result: variable 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. Lets call the number of pages "Z".

So me and my new friend Z want to tell some kind of function to fetch those articles. Ive made the following equations to find the start and the end of what articles I want to show.

$start = Z * 7 - 7

$end = Z * 7

Those equations generate

0 to 7 for page 1. Expected result (not reality):

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

8 to 15 for page 2. Expected result (not reality).

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:

 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.

 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!

 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.

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). So you should put:

array_slice($post_array, $start, 7);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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