首页 > 解决方案 > 在不同行中显示项目的 HTML 问题

问题描述

当我从数据库中读取图像时,我试图在每行中动态显示图像的三个副本,但它显示所有图像彼此相邻。我试图编辑代码以便复制它但没有用

无论数据库中的项目数量如何,如何在每行中显示三个图像?

这是我的代码:

<?php
    session_start();

    require "init.php";
    login();
?>
<html>
<head>
<title> Expert System </title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<table border='1' align='center'     >
<?php
// LOGIN USER
function login(){
    global $con;
    global $counter;

    //$email = $mysqli->escape_string('');
    $query="SELECT * FROM users ";
    $result=mysqli_query($con,$query);
    if ( $result->num_rows == 0 ) // User doesn't exist
        echo "User with that ID doesn't exist!";
    else { // User exists
        $counter=0;
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo '<td>
                <img src="images/' . $row["category"]. '" width="250px"  height= "150px" alt="Avatar" >
                </td>';
            echo '<td>
                    <img src="images/' . $row["category"]. '" width="250px"  height= "150px" alt="Avatar" >
                </td>';
            echo '<td>
                <img src="images/' . $row["category"]. '" width="250px"  height= "150px" alt="Avatar" >
                </td>';
            echo "</tr>";
        }
    }
}
mysqli_close($con);
?>
</table>

</body>
</html>

标签: phphtml

解决方案


根据与该问题所有者的长时间讨论,我正在发布我的解决方案。
在您的代码中,您只需在函数内 移动table 标记。

<?php
// LOGIN USER
function login(){
// write remaining code.... and then use this while loop
echo '<table border="1" align="center">';
while($row = $result->fetch_assoc()) 
   {   echo '<tr>';
       // If you want 2 images in a row than use $i<= 2
       for($i= 1; $i<= 3; $i++)
       {
        echo '<td><img src="images/' . $row["category"]. '" width="250px"  height= "150px" alt="Avatar" ></td>';   
       }
       echo '</tr>';
  } // while
} // login()


推荐阅读