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

时间:2013-04-28 16:25:48

标签: php

有人可以请求帮助,我在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>";
};
} 
}
?>

1 个答案:

答案 0 :(得分: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;”