简体   繁体   English

如何使用php获取存储在Mysql中的多个图像?

[英]How to fetch multiple images stored in Mysql using php?

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, 我使用逗号分隔的unique-Id将多个图像插入到表中,现在我想从数据库中获取这些图像并单独显示,

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.. 帮帮我们..

You forgot to re-initialize the image array 您忘了re-initialize 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. 因此,您将获得类别/后明智的第一张图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM