简体   繁体   中英

Display images from database into a Table on PHP

I have some problems creating a table that will display pictures from a php database

This is my code:

$query = "SELECT * FROM images ORDER BY name ASC ";

            $result = $db->query($query);
            $num_result = $result->num_rows;

            echo "<h1> Images</h1>";

            $array = array();
            for ($i = 0; $i < $num_result; $i++){

                $row = $result->fetch_assoc();
                $name = $row['name'];
                $URL = $row['imageURL'];


                $array[] = $URL;
            }
//this loop is printing the images correctly in order

foreach ($array as $image){
        echo '<img src="'.$image.'"/>';
          }  

What I am trying to accomplish is to create a table with 2 Columns that will print the images there, something like this

    echo '<table>';
    echo ' <tr>';
    echo '      <td>image 1</td>';
    echo '      <td>image 2</td>';
    echo '   </tr>';

    echo '   <tr>';
    echo '      <td>image 3</td>';
    echo '      <td> image 4</td>';
    echo '   </tr>';

      // and so on if there is more images

   echo '</table>';

Any suggestions will help , Thanks!

i think you want something like

$i=0;
echo"<table>";
echo"<tr>";
foreach ($array as $image)
{
    if($i%2==0 && $i>0 )
    echo"</tr><tr>";

    echo"<td>";
    echo '<img class="coupons" src="'.$image.'"/>';
    echo"</td>";
    $i++;
}
echo"</tr>";  
echo"</table>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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