php - 在不同行中显示项目的 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>
解决方案
根据与该问题所有者的长时间讨论,我正在发布我的解决方案。
在您的代码中,您只需在函数内 移动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()
推荐阅读
- spring-boot - 从 Spring Boot 1.5.x 升级到 2.0.x 时的 @Crossorigin 根中断
- php - 如何访问和修改外部文件中的 PHP 变量?
- php - 如何将图像从 React naitve 上传到 mysql 数据库?
- r - R矢量化重复循环
- php - 为什么使用 PHP 加载 URI 图像的速度非常慢
- python - PyQt5 QtSql 在 QThread 中访问数据库
- python - ModuleNotFoundError -> 导入的模块无法导入另一个模块
- python - OSError:[Errno 48] 地址已在使用中
- python - Python - 长 .txt 文件的文件夹,在每个 .txt 文件中搜索各种子字符串,附加到列表
- wordpress - 创建页面的 Wordpress 自定义帖子类型