分页:逻辑吗?

时间:2019-04-05 22:37:00

标签: php

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

目标:对文件数组进行分页(每页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 F Y @ H:i:s', $post_date)."<br>";
    }
}
?>

我认为这个问题是由我糟糕的逻辑技巧引起的。 谁能纠正我,让我指向文档?

非常感谢您:)

1 个答案:

答案 0 :(得分:0)

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

  

array_slice($ post_array,$ start,7);