-1

有人可以帮忙吗,我正在使用 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>";
};
} 
}
?>
4

1 回答 1

0

在你的 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;
};
} 
}
?>

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

于 2013-04-28T16:29:43.600 回答