将php脚本(获取目录中的所有照片)限制为7

将php脚本(获取目录中的所有照片)限制为7,php,Php,有人可以帮忙吗,我正在使用php脚本获取一个目录中的所有照片,但我希望它限制为总共只能获取7张图像,它选择的图像可以是随机的 谁能给我一个建议吗?谢谢 <?php if (isset($_SESSION['user_id'])) { if ($user['id'] == $_SESSION['user_id']){ if ($user['account_type'] == "Escort"){ ?> <div class="profil

有人可以帮忙吗,我正在使用php脚本获取一个目录中的所有照片,但我希望它限制为总共只能获取7张图像,它选择的图像可以是随机的

谁能给我一个建议吗?谢谢

<?php if (isset($_SESSION['user_id'])) { 
        if ($user['id'] == $_SESSION['user_id']){
            if ($user['account_type'] == "Escort"){ ?>
<div class="profile_photos_drop"><iframe src="includes/mod_photo_uploads/small_pics.php" width="184" height="194" scrolling="no" style="overflow:hidden; margin-top:-4px; margin-left:-4px; border:none;"></iframe></div><? } } } ?>
 <?php
$profile_bits = get_profile_bits();
while ($profile = mysql_fetch_array($profile_bits)) { 
$dirname = "./data/photos/".$profile_id."/";
$images = scandir($dirname);
$ignore = Array("_cover.jpg", "_default.jpg", "_starlight.jpg", "_starlight_thumb.jpg", "thumb_pic1.jpg", "thumb_pic2.jpg", "thumb_pic3.jpg", "thumb_pic4.jpg", "thumb_pic5.jpg", "thumb_pic6.jpg", "thumb_pic7.jpg", "thumb_pic8.jpg", "thumb_pic9.jpg", "thumb_pic10.jpg", "thumb_pic11.jpg", "thumb_pic12.jpg", "thumb_pic13.jpg", "thumb_pic14.jpg", "thumb_pic15.jpg", "thumb_pic16.jpg");
foreach($images as $curimg){
if(!in_array($curimg, $ignore) && preg_match("/\.jpg$/i", $curimg)) {
echo "<a href=\"".$dirname.$curimg."\" rel=\"shadowbox\" title=\"<strong>{$profile['display_name']}'s Photo's</strong>\"><img src='".$dirname.$curimg."' class=\"profile_photos\" width=\"170\" height=\"150\" ></a>";
};
} 
}
?>

在foreach循环中使用一个计数器,当它等于7时中断循环,如下所示:

<?php if (isset($_SESSION['user_id'])) { 
        if ($user['id'] == $_SESSION['user_id']){
            if ($user['account_type'] == "Escort"){ ?>
<div class="profile_photos_drop"><iframe src="includes/mod_photo_uploads/small_pics.php" width="184" height="194" scrolling="no" style="overflow:hidden; margin-top:-4px; margin-left:-4px; border:none;"></iframe></div><? } } } ?>
 <?php
$profile_bits = get_profile_bits();
while ($profile = mysql_fetch_array($profile_bits)) { 
$dirname = "./data/photos/".$profile_id."/";
$images = scandir($dirname);
$ignore = Array("_cover.jpg", "_default.jpg", "_starlight.jpg", "_starlight_thumb.jpg", "thumb_pic1.jpg", "thumb_pic2.jpg", "thumb_pic3.jpg", "thumb_pic4.jpg", "thumb_pic5.jpg", "thumb_pic6.jpg", "thumb_pic7.jpg", "thumb_pic8.jpg", "thumb_pic9.jpg", "thumb_pic10.jpg", "thumb_pic11.jpg", "thumb_pic12.jpg", "thumb_pic13.jpg", "thumb_pic14.jpg", "thumb_pic15.jpg", "thumb_pic16.jpg");
$counter = 0;
foreach($images as $curimg){
if(!in_array($curimg, $ignore) && preg_match("/\.jpg$/i", $curimg)) {
echo "<a href=\"".$dirname.$curimg."\" rel=\"shadowbox\" title=\"<strong>{$profile['display_name']}'s Photo's</strong>\"><img src='".$dirname.$curimg."' class=\"profile_photos\" width=\"170\" height=\"150\" ></a>";
$counter++;
if($counter == 7) break;
};
} 
}
?>


我只添加了这三行:“$counter=0;”、“$counter++”和“if($counter==7)break;”

$images=array\u slice(scandir($dirname),0,7)
。您正在迭代一个数组,如果您只想迭代7个项目,那么您应该简单地将数组裁剪为7个项目。这让我相信你根本没试过。那么,熊约翰怎么了?:-)