【发布时间】:2019-10-27 06:46:53
【问题描述】:
这是我的代码(我链接了一个外部 JS 和 jQuery 文件,我的图片文件夹称为 uploads/):
所以,目前这显示了我上传的所有图片。我想要的是限制它显示的图像数量。
我想限制它只允许显示最后 4 张图像。有办法吗?谢谢!
<?php include("header.inc"); ?>
<?php include("nav.inc"); ?>
<?php
$files = scandir("uploads/");
?>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<?php
$i = 0;
for($a = 2; $a < count($files); $a++):
?>
<li data-target="#myCarousel" data-slide-to="<?php echo $i; ?>" class="<?php echo $i == 0 ? 'active': ''; ?>"></li>
<?php
$i++;
endfor;
?>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
$i = 0;
for($a = 2; $a < count($files); $a++):
?>
<div class="item <?php echo $i == 0 ? 'active': ''; ?>" align="center">
<img src="uploads/<?php echo $files[$a]; ?>" style="width: 640px; height: 640px;">
</div>
<?php
$i++;
endfor;
?>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
<?php include("footer.inc"); ?>
<!-- end snippet -->
【问题讨论】:
-
执行后 $files = scandir("uploads/");您应该根据需要过滤文件数组(在您的情况下获取最新的 4 个修改文件)。例如:jonasjohn.de/snippets/php/listdir-by-date.htm
标签: javascript php html carousel