Php foreach循环,根据if/else添加到不同的div

Php foreach循环,根据if/else添加到不同的div,php,html,Php,Html,我当前的代码: <?php if($_images){?> <?php $i=0; foreach($_images as $_image){ $i++; ?> <?php if($i > 2) { ?> <div class = "largePic"> <img src="<?php echo $this->helper(

我当前的代码:

<?php if($_images){?>           
    <?php $i=0; foreach($_images as $_image){ $i++; ?>
        <?php if($i > 2) { ?>
            <div class = "largePic">
                <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
            </div>
        <?php } else { ?>
            <div class = "smallPic">
            <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
            </div>
        <?php } ?>

    <?php } ?>  
<?php } ?>  

helper('catalog/image')->init($_product,'thumbnail',$_image->getFile())->resize(600900);?>“alt=”“title=”“/>
helper('catalog/image')->init($_product,'thumbnail',$_image->getFile())->调整大小(450675);?>“alt=”“title=”“/>
这显然是错误的,因为每次回显图像时,它都被分配到不同的div(相同的名称,但不同)。是否可以根据计数将回声分配给特定的div


例如,前两个图像将分配给smallPic div,然后剩下的图像将分配给largePic div。

我不懂PHP,但我非常确定,对于start,您可以松开那些广泛的open/close标记(
),并在开始和结束处仅用一个替换它。这很难理解

然后,就像在许多服务器端脚本中一样,您需要的是某种包含HTML的缓冲区,您将在循环结束时输出该缓冲区。因为您现在所做的是从循环内部输出,这将向客户机发送多个div


创建包含DIVs HTML的
largePic
nad
smallPic
变量,并将HTML附加到循环中(而不是像现在一样输出到客户端)。然后,在循环完成后,将这两个变量输出到客户端。

您需要的是首先将数组分为两个数组,然后通过这两个数组进行foreach

<?php

$largeImages = array();
$smallImages = array();

foreach ($_images as $k => $v) {
    if ($k > 2) {
        $largeImages[] = $v;
    } else {
        $smallImages[] = $v
    }
}
?>
<div class = "largePic">
<?php foreach ($largeImages as $_image) { ?>
    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
</div>
<div class = "smallPic">
<?php foreach ($smallImages as $_image) { ?>
    <img src="<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675); ?>" alt="<?php echo $this->htmlEscape($_image->getLabel());?>" title="<?php $this->htmlEscape($_image->getLabel());?>" />
<?php } ?>
</div>

helper('catalog/image')->init($_product,'thumbnail',$_image->getFile())->resize(600900);?>“alt=”“title=”“/>
helper('catalog/image')->init($_product,'thumbnail',$_image->getFile())->调整大小(450675);?>“alt=”“title=”“/>
首先处理图像(将每个图像的HTML存储在数组或字符串中),然后创建div元素-参见下面的示例。这将大大减少打开/关闭标记的数量

还请注意,如果数组的键是数字的,则可以使用以下语法,而不是创建额外的变量(即
$i
)来跟踪索引(以避免额外的簿记,如增加变量,这是与语句相比的主要优点之一)

foreach(数组\表达式为$key=>$value)
声明


现在有什么问题吗?只需将div放在foreach之外,改用2 foreach(将$\u images数组中的图片分配给div时将其删除)。现在的问题是,前两个图像被读取为两个不同的div,因此它们排列在彼此的顶部,而不是紧挨着彼此@听起来这更像是一个样式问题,而不是PHP问题。为什么不使用CSS使它们彼此相邻?我可以,但看到一堆同名的div占据了空间还是很混乱的。如果可能的话,我想把他们分成一个小组。
<?php 
$largePic = '';
$smallPic = '';
if($_images){      
    foreach($_images as $i => $_image){ 
        if($i > 2) { 
           $largePic .= '<img src="'.$this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(600,900).'" alt="'. $this->htmlEscape($_image->getLabel()). '" title="'. $this->htmlEscape($_image->getLabel()).'" />';
         } else { 
            $smallPic .= '<img src="'. $this->helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(450,675). '" alt="'. $this->htmlEscape($_image->getLabel()). '" title="'. $this->htmlEscape($_image->getLabel()). '" />';
        } 
    }  
 } ?> 
<div class = "largePic"><?php echo $largePic; ?></div>  
<div class = "smallPic"><?php echo $smallPic; ?></div>