如何限制图像轮播中可以显示的图像数量? (PHP)
How do I set a limit on how many images can be shown in an image carousel? (PHP)
这是我的代码(我有一个外部 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/");
与:
const CAROUSEL_LENGTH = 4; // Should be on top of script or in a config file
$files = array_slice(scandir('uploads/', SCANDIR_SORT_DESCENDING), 0, CAROUSEL_LENGTH);
SCANDIR_SORT_DESCENDING
标志将按时间戳对文件进行排序(参见 this post for a more in-depth explanation) and array_slice
将取前四个。
这是我的代码(我有一个外部 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/");
与:
const CAROUSEL_LENGTH = 4; // Should be on top of script or in a config file
$files = array_slice(scandir('uploads/', SCANDIR_SORT_DESCENDING), 0, CAROUSEL_LENGTH);
SCANDIR_SORT_DESCENDING
标志将按时间戳对文件进行排序(参见 this post for a more in-depth explanation) and array_slice
将取前四个。