循环模式中的PDO循环图像

时间:2018-03-07 08:26:15

标签: php pdo

期望的结果:在我的数据库table1中循环等量的模态作为ID,同时在这些循环模式中成功循环table2中的图像。

当前结果:循环一个模态期间,同时在一个成功的模态中循环table2中的所有图像。

做什么,怎么做?

                    <?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">&times;</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: Table 1 (comment_imgs)

表2:Table 2 (Uploads)

1 个答案:

答案 0 :(得分:0)

将内部循环中的结果变量$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