与使用三个SQL查询将信息从数据库拉入html列冲突



下面的代码在视觉上几乎完全符合我的要求。除了第1列和第2列,所有10个条目都显示相同的图片,而第3列每行显示一个唯一的条目。

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Covid-Deaths</title>
<link rel="stylesheet" type="text/css" href="covid.css">
<script src="https://kit.fontawesome.com/1f285a5a86.js" crossorigin="anonymous"></script>
<script src="http://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="script.js"></script> 
</head>
<body>
<h1>You're terrible at this</h1>
<p>Get each picture to be unique</p>
<?php
//database Connection
include 'dbconfig.php';
// retrieving data from table accounts
$query  = "SELECT * FROM test_info where tf = 1 LIMIT 10";
$result = mysqli_query($conn, $query);
$query2  = "SELECT * FROM test_info where tf = 1 LIMIT 10 OFFSET 10";
$result2 = mysqli_query($conn, $query2);
$query3  = "SELECT * FROM test_info where tf = 1 LIMIT 10 OFFSET 20";
$result3 = mysqli_query($conn, $query3);
?>
<?php 
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
if ($result2->num_rows > 0) {
while ($row2 = $result2->fetch_assoc()) {
if ($result3->num_rows > 0) {
while ($row3 = $result3->fetch_assoc()) {
?>
<div class="container">
<div class="card">
<a href="<?php echo $row['obit_url'];  ?>"><img src="uploaded-images/<?php echo 
$row['picture'];?>" width=80%/></a>
<h4><?php echo $row['names']; echo ", "; echo $row['age'];?></h4>
<p><?php echo " "; echo "State: "; echo $row['state']; ?></p>                
</div>
<div class="card">
<a href="<?php echo $row2['obit_url'];  ?>"><img src="uploaded-images/<?php echo 
$row2['picture'];?>" width=80%/></a>
<h4><?php echo $row2['names']; echo ", "; echo $row2['age'];?></h4>
<p><?php echo " "; echo "State: "; echo $row2['state']; ?></p>                
</div>
<div class="card">
<a href="<?php echo $row3['obit_url'];  ?>"><img src="uploaded-images/<?php echo 
$row3['picture'];?>" width=80%/></a>
<h4><?php echo $row3['names']; echo ", "; echo $row3['age'];?></h4>
<p><?php echo " "; echo "State: "; echo $row3['state']; ?></p>                
</div>

</div>
<?php
}
}
}
}
}
}
?>
</body>
</html>

输出示例:

Steve Paul Mary

Steve Paul John

Steve Paul Sam

我想要它的样子:

Steve Paul Mary

Randy Kyle John

Phil Scott Sam

我似乎无法从第一列和第二列中提取我所知道的唯一条目。

在中间的while循环中,第一列和第二列将保持不变,因为您没有为它们获取新行。

您可以将其更改为1个while循环,然后只获取该1循环中的其他行。。。

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$row2 = $result2->fetch_assoc();
$row3 = $result3->fetch_assoc();

这将以相同的顺序获取所有3个结果。但是,如果其中任何一个不返回值,您必须知道该怎么办。