0

我有以下foreach:

<?php 
foreach($result15 as $row15) { 
   $thumb15 = $row15->thumb;
   $id15 = $row15->id_discografia;
?>
<div class='wrapper'>
   <div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246"></div>
</div>  

<?php } ?>

但因此在每个 div .wrapper 中只出现一个 div .album。如何在每个 div .wrapper 中看到两个 div .album?

更新

伙计们,找到了解决方案:

<?php
$total = 0;
foreach($result15 as $row15){ 
$thumb15 = $row15->thumb;
$id15 = $row15->id_discografia;

if($total == 0){
    echo '<div class="wrapper">';
}        
?>
<div class="album" data-disco="disco<?php echo $id15; ?>">
     <img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
</div>
<?php
$total = $total + 1;

if($total % 2 == 0){
    echo '</div>';
    $total = 0;
}
}
 ?>
4

3 回答 3

0

尝试这个:

<?php 
foreach($result15 as $row15) { 
   $thumb15 = $row15->thumb;
   $id15 = $row15->id_discografia;

echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$id15 .' alt="" width="246" height="246"></div>';
echo '</div>';  
 } ?>
于 2013-06-24T13:05:58.390 回答
0
<div class='wrapper'>
   <div class="album"><img src="img/<?php echo $thumb15; ?>" alt="" width="246" height="246">
   <div class="album"><img src="img/..." alt="" width="246" height="246"></div>
</div>

像这样的东西?

编辑

我不明白你想要什么。但是您可以这样做来获取两个 div,但是您需要获取第二个 div 图像的图像路径:

<?php 
foreach($result15 as $row15) { 
   $thumb15 = $row15->thumb;
   $id15 = $row15->id_discografia;

echo "<div class='wrapper'>";
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '<div class="album"><img src="img/'.$thumb15.' alt="" width="246" height="246"></div>';
echo '</div>';  
 } ?>
于 2013-06-24T12:51:29.933 回答
0

考虑到您想一次处理 2 个图像的“块”中的数据,一个不错的解决方案可能是使用数组块。这样,如果你想改变包装器中出现的图像数量,你只需要改变你的块大小。

$chunkSize = 2;
foreach (array_chunk($result15, $chunkSize) as $wrapperImages) {
    echo '<div class="wrapper">';
    foreach ($wrapperImages as $image) {
        $thumb = $image->thumb;
        echo '<div class="album"><img src="img/'.$thumb.' alt="" width="246" height="246"></div>';
    }
    echo '</div>';
}
于 2013-06-24T13:10:44.773 回答