此脚本获取文件夹中的每个图像并将其显示在网页上。有没有一种方法可以像一个简单的页码一样显示,比如每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";
}
?>
既然你说你不是PHP
大师,我将展示我的解决方案并逐步解释
我希望你能觉得它有帮助。
这是我用于分页的循环:
for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
}
我将使用它创建一个新的数组,其中包含11-20、21-30 首先,我删除了 因为这会使索引有点混乱(这对于下一步来说是必要的)。 然后,我创建了一个带有0到N索引的新数组,这使代码整洁(我这样做是为了尽可能少地更改代码),然后用 最后是分页的实现: 变量: 循环: 然后,您只需要将代码第29行中要循环的数组从 对于分页控制,请使用以下内容: 以下是新的代码实现: 对于页面编号,你必须知道元素的总数,并将其除以 来自php.net 如有必要,通过四舍五入值返回下一个最高整数值。 然后使用它来显示钉号:$sortedImages
数组索引中的.$i
(代码的第15行)for ($i = 0; $i < $count; $i++) {
$sortedImages[date ('YmdHis', filemtime($images[$i]))] = $images[$i]; #15th Row
}
$sortedImages
数组的元素填充它。$k = 0; # The new index
$newArray = array(); # The new array
foreach($sortedImages as $soImg) {
$newArray[$k] = $soImg;
$k++;
}
$page = $_GET["page"];
$perPage = 3;
$total = $count;
for ($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
$newSortedImages[$i] = $newArray[$i];
}
$page = $_GET["page"];
是从url检索的页码($_GET[]
是超全局数组)$perPage
是每页要显示的元素数$total = $count;
是$images
阵列的编号(第13行)$i = (($page-1)*$perPage)
是循环的开始,如果页面为1,则循环应从0开始,则表达式(($page-1)*$perPage)
使其工作$i < min(($page*$perPage), $total)
是循环的结束,min()
函数在其参数之间找到最低值,例如,当最后一页包含4个元素,而预期为6个元素时,这很有帮助$sortedImages
更改为$newSortedImages
。$nextPage = $page + 1;
$prevPage = $page - 1;
# 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);
}
# Now I give an index from 0 to N to the new array to make it work with pagination loop
$k = 0; # The new index
$newArray = array(); # The new array
foreach($sortedImages as $soImg) {
$newArray[$k] = $soImg;
$k++;
}
$page = $_GET["page"];
$perPage = 3;
$total = $count;
for ($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
$newSortedImages[$i] = $newArray[$i];
}
# Generate the HTML output
writeHtml('<ul class="ins-imgs">');
foreach ($newSortedImages 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";
}
$perPage
元素,显然结果必须是一个整数,所以你将使用ceil()
函数$pages = ceil($count / $perPage);
for($i = 1; $i <= $pages; $i++) {
writeHtml('<a href="?page=' . $i . '">' . $i . '</a> ');
}
您不需要数据库来实现简单的分页。
$currentpage = $_GET['p'];
for ($i = ($currentpage*$pageSize); $i<sortedImages as $image) {
// your HTML output here
}
for($i = 0; $i < $sortedImages/$pageSize; $i++){
writeHtml('<a href=/?p='.$i + 1.'/>');
}
这只是一个例子,如果你剪切粘贴,可能不会起作用。但这是为了给你一个如何实现它的好主意。