2016-07-13 15 views
3

我已經使用由逗號分隔的唯一ID插入多個圖像,現在我想要從數據庫中獲取這些圖像並單獨顯示,如何使用PHP獲取存儲在Mysql中的多個圖像?

在數據庫中,圖像以類別方式存儲,每個類別具有以下格式的圖像。

142375784eb96ef5671468328855.jpg,133305784eb96ef9f01468328855.jpg,224995784eb96efcb31468328855.jpg 

我曾嘗試下面的代碼

<?php 
$query = "SELECT * FROM book_post"; 
$res = mysqli_query($con, $query); 
    if(mysqli_num_rows($res) > 0) 
     { 
      while($row = mysqli_fetch_assoc($res)) 
      { 
    $temp = array(); 
    $row['book_image']=trim($row['book_image'], '/,'); 
    $temp = explode(',', $row['book_image']); 
    $temp = array_filter($temp); 
    foreach($temp as $image){ 
    $images[]="images/book/".$row['book_category']."/".trim(str_replace(array('[',']') ,"" ,$image)); 
    } 
?> 
<div class="col-sm-4"> 
    <div class="product-image-wrapper"> 
    <div class="single-products"> 
     <div class="productinfo text-center"> 
     <img src="<?php echo $images[0];?>" alt="" /> 
      <h2>&#8377;<?php echo $row['book_price'];?></h2> 
      <p><?php echo $row['book_name'];?></p> 
     </div> 
     <div class="product-overlay"> 
     <div class="overlay-content"> 
      <h2><?php echo $row['book_name'];?></h2> 
      <p>Author: <?php echo $row['book_author'];?></p> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 
<?php } 
} 
?> 

這裏的問題是,它獲取的第一印象,並在每個類別中顯示它。我想顯示在類別中的圖像明智

幫助我出去傢伙..

+0

目前您獲得所有類別的相同(第一個)圖像 –

+0

是的,我想按類別顯示。可能嗎? – Prashanth

+1

當然。看到你如何對你的模式進行零洞察,並且由於我們不是通靈,你將不得不更好地解釋(提示:「SHOW CREATE TABLE」可以幫助我們很多)或者自己弄清楚。 – tadman

回答

0

你忘了re-initializeimage array

所以更改以下

foreach($temp as $image){ 
    $images[]="images/book/".$row['book_category']."/".trim(str_replace(array('[',']') ,"" ,$image)); 
} 

進入

$images = array(); 
foreach($temp as $image){ 
    $images[]="images/book/".$row['book_category']."/".trim(str_replace(array('[',']') ,"" ,$image)); 
} 

所以你會得到第一個圖像的類別/明智的。

相關問題