PHP页面编号

时间:2015-03-27 10:12:45

标签: php

此脚本获取文件夹中的每个图像并在网页上显示。有没有办法像每1,2张图片一样显示像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";



    }

?>

1 个答案:

答案 0 :(得分:1)

您需要计算该文件夹中的图像总数,将其分成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

}