foreach循环 - 具有不同设置的案例

时间:2017-04-12 15:06:57

标签: php foreach

我有一个foreach循环给出了一个风景画廊图像的结果。 基本上以

开头
<?php foreach ( $images as $image ) : ?>

然后是图库图像的常规div + anchor +图像标记。

<?php if ( $image->description == "portrait" ) :  ?>

<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box portrait" <?php echo $image->style ?> >


<div class="ngg-gallery-thumbnail" >

        <a class="portrait-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >           
        <img title="<?php  echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
        </a>

<?php else: ?>

<div id="ngg-image-<?php echo $image->pid ?>" class="ngg-gallery-thumbnail-box landscape" <?php echo $image->style ?> >


<div class="ngg-gallery-thumbnail" >

        <a class="landscape-image" href="<?php echo $image->imageURL ?>" title=" " id="<?php echo $image->alttext ?>" rel="lightbox" <?php echo $image->thumbcode ?> >          
        <img title="<?php  echo ' ' /* $image->alttext */ ?>" alt="<?php echo ' ' /* $image->alttext */ ?>" src="<?php echo $image->thumbnailURL ?>" <?php echo $image->size ?> />
        </a>


<?php endif; ?>

我想要特殊情况,我有两个并排的肖像图像,填充一个风景图像的空间。

我在考虑的是在管理区域中有一个标签,它会触发foreach循环的中断,在一个容器div中有两个图像,然后它将继续使用容器+图像的常规循环对于风景图像。 在foreach循环的情况下,是否可以跳出循环,使用不同的设置创建一个案例,然后返回到常规循环方法?

3 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

foreach($images as $key => $image) {
    if($image has special tag) { // <--- PSEUDOCODE 
        echo "<div class='SPECIAL_CSS_CLASS'><img src='" . $img->path . "' /></div>";
    }
}

然后在你的CSS中做你的魔法!

答案 1 :(得分:1)

我会继续做这样的事情:

//Function to get all the images based on a specific portrait_group
function getImagesPortraitGroup($group, $images) {
    $result = array();
    foreach ($images as $image) {
        if (isset($image['portrait_group']) && $image['portrait_group'] == $group) {
            $result[] = $image;
        }
    }
    return $result;
}

//The images to show
$images = array(
    ["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image1", "portrait_group" => 1],
    ["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image2", "portrait_group" => 1],
    ["url" => "http://lorempixel.com/800/200/", "caption" => "Lorem Image3"],
    ["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image4", "portrait_group" => 2],
    ["url" => "http://lorempixel.com/400/200/", "caption" => "Lorem Image5", "portrait_group" => 2],
);

//Everytime we show a group, we track it by adding it to this array in order not to show it again
$addedGroups = array();

//Loop through all the images
for ($i = 0; $i < count($images); $i++) {
    echo "<div>";

    //If the image has a portrait_group that is not already shown, show it
    if (isset($images[$i]['portrait_group']) && !in_array($images[$i]['portrait_group'], $addedGroups)) {
        $groupImages = getImagesPortraitGroup($images[$i]['portrait_group'], $images);
        foreach ($groupImages as $image) {
            echo "<img src='{$image['url']}' title='{$image['caption']}' >";
        }
        //Save the group to the array in order not to show it again
        $addedGroups[] = $images[$i]['portrait_group'];
    } else {
        echo "<img src='{$images[$i]['url']}' title='{$images[$i]['caption']}' >";
    }
    echo "</div>";
}

答案 2 :(得分:0)

我设法通过jQuery进行分组,这是代码

jQuery(".ngg-gallery-thumbnail-box.portrait").filter(":odd").each(function(){
    $content = jQuery(this).find("a");
    jQuery(this).prev().find(".ngg-gallery-thumbnail").append($content);
    jQuery(this).remove();
});