数组中用于保存文件名的数组


Arrays inside array to hold filenames

这是针对网站的首页,它应该在最顶部显示最新/最新的帖子,当您向下滚动并转到下一页时,帖子会变旧。

有一个文件夹包含许多ini文件,名称中只有数字。我正在尝试做的是将所有帖子名称(并且仅名称 - 而不是其内容)加载到一个数组中,然后将其排序到其他数组中。我想也许使用多维数组是个好主意。例如(如果我理解这个权利),$usePage[1][2]将在第一页上有第二个帖子的编号。甚至是最好的方法吗?

以下是相关的代码:

$ppp = 4;
$totalposts = 0;
$posts = array();
foreach (scandir($postsLoc) as $file) {
        if (is_file($postsLoc . "/" . $file)) {
        $totalposts++;
        array_push($posts, $file);
    }
}
natsort($posts);
array_values($posts);
$posts = array_reverse($posts);
print_r($posts);
$currPage = -;
$usePage = array(array());
$done = 0;
for ($i = $totalposts; $i != 0; $i--){
    if ($done >= $ppp){
        //Next page
        $currPage++;
        $done = 0;
        $usePage[$currPage] = array();
    }
    $done++;
    array_push($usePage[$currPage][$done], $i);
}
print_r($usePage);

到目前为止,我已经设法混淆了自己。

提前感谢您的任何帮助!

下面的代码会产生一个多维$postsInPage数组,第一个维度是页面引用,第二个维度是该页面的帖子。然后,您应该能够使用此数组提取依赖于您的 pageId 的相关帖子:

Array
(
    [1] => Array
        (
            [0] => .
            [1] => ..
            [2] => email_20131212_2c7a6.html
            [3] => email_20131212_98831.html
        )
    [2] => Array
        (
            [0] => errFile_20140110_940ad.txt
            [1] => errFile_20140110_2021a.txt
            [2] => errFile_20140110_2591c.txt
            [3] => errFile_20140110_43280.txt
等等

等等。代码(不包括is_file检查)

// load all the posts into an array:
$allPosts = array();
foreach (scandir("temp") as $file) {
        $allPosts[] = $file;
}
//sort the array (am making an assumption that $file structure will natsort sensibly
natsort($allPosts);
$allPosts = array_values($allPosts);
//split into posts per page.
$ppp = 4;
$pageId = 1;
$totalposts = 1;
$postsInPage = array();
foreach ($allPosts as $post) {
    $postsInPage[$pageId][] = $post;
    if (($totalposts % $ppp) == 0) { //i.e. 4 per page
        $pageId++;
    }
    $totalposts++;
}