显示div的2结果

时间:2013-06-24 12:47:23

标签: php pdo 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;
}
}
 ?>

3 个答案:

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

答案 1 :(得分: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>';  
 } ?>

答案 2 :(得分:0)

一个好的解决方案可能是使用array chunk,考虑到你想要一次处理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>';
}