2013-06-24 48 views
0

我有以下的foreach:顯示2分的結果通過DIV

<?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?

UPDATE

夥計們,找到了解決辦法:

<?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; 
} 
} 
?> 
+0

嘗試在foreach循環中使用echo – blackuprise

+0

@blackuprise我相信我需要一個計數器。但我不知道如何使用它 – user2433958

回答

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>'; 
} ?> 
+0

耶!這樣的事情:一個.wrapper每兩個.album div – user2433958

+0

@ user2433958所以只需添加我有你的PHP。 – stackErr

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>'; 
} ?> 
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>'; 
}