PHP页面编号


PHP page numbering

此脚本获取文件夹中的每个图像并将其显示在网页上。有没有一种方法可以像一个简单的页码一样显示,比如每10张图片显示1、2、3、4、5?我试着用谷歌搜索一些东西,但我能找到的只是一个数据库。使用数组是否可能?因为此时它加载了1000个图像,导致系统崩溃

<?php
    # To prevent browser error output
    header('Content-Type: text/javascript; charset=UTF-8');
    # Path to image folder
    $imagefolder = 'img/';
    # Show only these file types in the image folder
    $imagetypes = '{*.jpg,*.JPG,*.JPEG,*.png,*.PNG,*.gif,*.GIF}';
    # Add images to array
    $images = glob($imagefolder.$imagetypes, GLOB_BRACE);
    # Sort the images based on its 'last modified' time stamp
    $sortedImages = array();
    $count = count($images);
    for ($i = 0; $i < $count; $i++) {
        $sortedImages[date ('YmdHis', filemtime($images[$i])).$i] = $images[$i];
    }
    # Set to 'false' if you want the oldest images to appear first
    $newest_images_first = true;
    # Sort images in array
    if($newest_images_first) {
        krsort($sortedImages);
    } else {
        ksort($sortedImages);
    }
    # Generate the HTML output
    writeHtml('<ul class="ins-imgs">');
    foreach ($sortedImages as $image) {
        # Get the name of the image, stripped from image folder path and file type extension
        $name = 'Image name: '.substr($image,strlen($imagefolder),strpos($image, '.')-strlen($imagefolder));
        # Get the 'last modified' time stamp, make it human readable
        $last_modified = '(last modified: '.date('F d Y H:i:s', filemtime($image)).')';
        # Begin adding
        writeHtml('<li class="ins-imgs-li">');
        writeHtml('<div class="ins-imgs-label">'.$name.' '.$last_modified.'</div>');
        writeHtml('<div class="ins-imgs-img"><a name="'.$image.'" href="#'.$image.'">');
        writeHtml('<img src="'.$image.'" alt="'. $name.'" title="'. $name.'">');
        writeHtml('</a></div>');
        writeHtml('</li>');
    }
    writeHtml('</ul>');
    writeHtml('<link rel="stylesheet" type="text/css" href="ins-imgs.css">');
    # Convert HTML to JS
    function writeHtml($html) {
        echo "document.write('".$html."');'n";

    }
?>

您需要计算该文件夹中的图像总数,将其拆分为10个部分,然后迭代该数量的图像,而不是迭代整个图像。

total_images = 1000
limit = 10
parts = ceil(total_images/10)
foreach(parts as count) {
 offset = limit * ( count - 1)
 // iterate over images ranging from limit, offset
}