Home » Php » css – How can I retrieve and echo multiple images into php without using tables

css – How can I retrieve and echo multiple images into php without using tables

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have tried to echo multiple images from the same post_id into a certain style.

I am retrieving information, text, and multiple images from database using mysqli and php. These are my tables:

TABLE 
    posts(
    post_id INT(15) NOT NULL AUTO_INCREMENT,
    user_id INT(11) NOT NULL,
    post_text VARCHAR(400) NOT NULL,
    post_file VARCHAR(200) NOT NULL,
    PRIMARY KEY(`post_id`),
    KEY `user_id`(`user_id`)
)

TABLE 
    user_images(
    post_id INT(15) NOT NULL AUTO_INCREMENT,
    user_id INT(11) NOT NULL,
    album_id INT(11) NOT NULL,
    image_name VARCHAR(200) NOT NULL,
)

TABLE 
    user_video: same as above but instead of image_name it is video_name.

I have successfully retrieved the information and images and placed into tables. However, I am unable to style the table as I would div, span or even lists. I have retrieved the images and information into the for mentioned, but I an unable to style them as I want and the not all the images, for example, are inside the div or span or div that contains the list.

I want to retrieve them into the following format or another that I can do with css: This is an example of what I want to achive

I will also retrieve video formats into them. If you can help with that, great if not, I will figure it out one way or another.

This is the code I have using lists.

//CODE USING TABLES

while ($recrplu = mysqli_fetch_array($results_plu, MYSQLI_NUM)) {

    $usid = $rfl[1];
    $post_id = $recrplu[0];
    $post_text = $recrplu[2];

    $img_search = mysqli_query($conn, "SELECT * FROM user_images WHERE post_id='$post_id'");
    echo '
        <tr>
            <td>'.$usid.'
            </td>
            <td>'.$post_id.'
            </td>
            <td>
                '.$post_text.'
            </td>';
    while($rec_images = mysqli_fetch_array($img_search, MYSQLI_NUM))
    {
        $post_image = $rec_images[3];
?>
            <td width="220" >
                <div style="height: 300px; border: 1px solid black; ">
                    <?php 
                        $images = explode(",",$post_image);
                        foreach($images as $image)
                        {
                        ?>
                            <img src="<?php echo 'upload/'.$image; ?>" width="220" height="220">
                    <?php
                        }
                    ?>
                </div>
            </td>        
<?php
}
        }
echo "</table>";                
?>


//CODE USING LISTS
<?php
        $post_lookup2 = "SELECT * FROM posts WHERE user_id = '$usid'";
        $results_plu2 = mysqli_query($conn, $post_lookup2);
while ($recrplu2 = mysqli_fetch_array($results_plu2, MYSQLI_NUM)) {
    $usid = $rfl[1];
    $post_id = $recrplu2[0];
    $post_text = $recrplu2[2];
    $img_search1 = mysqli_query($conn, "SELECT * FROM user_images WHERE post_id='$post_id'");
?>
<br><br>
        <div style="width: 800px; height: 400px; border: 1px solid black;">
        <div style="width: 100px;"><?php echo $usid;        ?></div>
        <div style="width: 100px;"><?php echo $post_id;     ?></div>
        <div style="width: 100px;"><?php echo $post_text;   ?></div>
<?php
    while($rec_images2 = mysqli_fetch_array($img_search1, MYSQLI_NUM))
    {
        $post_image2 = $rec_images2[3];
?>
        <ul >
            <?php 
                $images2 = explode(",",$post_image2);
                foreach($images2 as $image2)
                {
                ?>
                <li>
                    <img src="upload/<?php echo $image2; ?>" width="110" height="110">
                </li>         
            <?php
                }
            ?>
        </ul>    
</div>   
<?php
    }
}              
?>

How to&Answers: