4

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>&#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 }
}
?>

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

3
  • Currently you get same(first) image for all category Commented Jul 13, 2016 at 6:51
  • yes, I want to display by category wise. Is it possible? Commented Jul 13, 2016 at 8:02
  • 1
    Sure. Seeing as how you've given zero insight into what your schema is, and as we're not psychic, you're going to have to explain better (Hint: SHOW CREATE TABLE helps us a lot) or figure it out yourself. Commented Jul 13, 2016 at 9:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.