Reputation: 338
I have inserted the multiple images to the table using the unique-Id separated by commas and Now I want fetch those images from the database and display it separately,
In database the images are stored in category wise, each category having the images in following format.
142375784eb96ef5671468328855.jpg,133305784eb96ef9f01468328855.jpg,224995784eb96efcb31468328855.jpg
I have tried the following code
<?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>₹<?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 }
}
?>
Here the problem is, It fetching the first images and displaying it in every category. I want to display the images in category wise
Help me out guys..
Upvotes: 4
Views: 3146
Reputation: 2195
You forgot to re-initialize
the image array
So change the following
foreach($temp as $image){
$images[]="images/book/".$row['book_category']."/".trim( str_replace(array('[',']') ,"" ,$image ) );
}
Into
$images = array();
foreach($temp as $image){
$images[]="images/book/".$row['book_category']."/".trim( str_replace(array('[',']') ,"" ,$image ) );
}
So you will get first image for category/post wise.
Upvotes: 2