期望的结果:在我的数据库 table1 中循环等量的模态作为 ID,同时在这些循环模态中成功循环表中 2 中的图像。
当前结果:循环一个模态周期,同时在一个成功的模态内循环表 2 中的所有图像。
做什么,怎么做?
<?php
include("dbconfig.php");
/*include("class.user.php");*/
$user_id = $_SESSION['user_session'];
$user_name = $_SESSION['user_name'];
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$id = $row['id'];
$date = $row['date'];
$comment = $row['comment'];
$project_id = $row['project_id'];
$display_id = $row['display_id'];
$user_name = $row['user_name'];
?>
<div class="card" style="width: 18rem;" id="display">
<div class="card-body">
<h5 class="card-title"><?php echo $project_id;?></h5>
<p class="card-text"><?php echo $date;?></p>
<p class="card-text"><?php echo $user_name;?></p>
<p class="card-text"><?php echo $comment;?></p>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#exampleModal<?php echo $id;?>" id="formButtons">
Button
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal<?php echo $id;?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModal<?php echo $id;?>Label"><?php echo $project_id;?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php echo $date;?>
<?php echo $comment;?>
<?php echo $user_name;?>
<?php
$stmt = $DB_con->prepare("SELECT * FROM uploads");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$image_path = $row["image_path"]."/".$row["image_name"];
$display_id = $row['display_id'];
?>
<a href="<?php echo $image_path; ?>"><img src="<?php echo $image_path; ?>" class="images" /></a><?php } ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
表1:表1(comment_imgs(
表2:表2(上传(
将内部循环中的结果变量$stmt
更改为其他变量,因为您要覆盖正在迭代的变量。
$stmt = $DB_con->prepare("SELECT * FROM comment_imgs");
$stmt->execute();
$stmt = $DB_con->prepare("SELECT * FROM uploads");
$stmt->execute();
// the variable result is overwritten