首页 > 解决方案 > 暂停循环,直到使用 PHP 单击按钮

问题描述

目前,我有一个页面显示当前目录中的所有电影及其缩略图。为了帮助加载时间,我想暂停foreach循环,直到单击“更多”,然后在同一页面上加载接下来的 20 页,或将其拆分为“第 1、2、3...”。

我开始了,然后意识到这只是运行了foreach20 次:

$movies = glob('./*.{mp4,m4v}', GLOB_BRACE);
$images = glob('./*.{jpeg,jpg,png}', GLOB_BRACE);
$subtitles = glob('./*.{srt}', GLOB_BRACE); #To be used later.
$dir = $_SERVER['DOCUMENT_ROOT'];
$results = 0;
$resultsMax = 20;

echo file_get_contents($dir . "/html/header.html");

while ($results < $resultsMax) {
    foreach ($images as $image) {
        $lookup[pathinfo($image, PATHINFO_FILENAME)] = $image;
    }

    foreach ($movies as $movie) {
        $image = $lookup[pathinfo($movie, PATHINFO_FILENAME)] ?? 'Temporary.jpg';
        echo '<a href="' . $movie . '">
                    <img src="' . $image . '" style="width:300px;height:350px;border:0;">
                </a>';
    }
    $results++;
}
echo file_get_contents($dir . "/html/footer.html");

我该如何解决这个问题?

标签: php

解决方案


推荐阅读