此脚本获取文件夹中的每个图像并将其显示在网页上。有没有办法像每 10 张图像显示 1,2,3,4,5 这样的简单页码?我尝试谷歌一些东西,但我能找到的只是一个数据库。数组甚至可能吗?
<?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";
}
?>
当然。使用 GET 变量移动页面:
$perPage = 5;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $perPage;
$end = ($page * $perPage) - 1;
for ($i = $start; $i <= $end; $i++) {
$image = $sortedImages[$i];
// rest of your code
}
或
$perPage = 5;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start = ($page - 1) * $perPage;
$images = array_slice($sortedImages, $start, $perPage);
foreach ($images as $image) {
// rest of your code
}
对于下一页链接,只需执行<a href="?page=<?php echo $page + 1; ?>">Next</a>
。应该能够解决其余的问题。