【问题标题】:PDO Loop images within a looped modal [duplicate]循环模态中的 PDO 循环图像 [重复]
【发布时间】:2018-08-15 06:39:34
【问题描述】:

期望的结果:在我的数据库 table1 中循环与 ID 相同数量的模态,同时在这些循环的模态中成功循环来自 table2 的图像。

当前结果:循环一个模态 period,同时在一个成功的模态中循环 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)

table2:Table 2 (Uploads)

【问题讨论】:

  • 您对 2 个请求使用了两次相同的 var $stmt。第二个覆盖第一个,并且由于所有行都已在内部循环中提取,因此主循环结束,不再获取行。 (你也是用了两次$row,目前没有效果,但也要改)

标签: php pdo


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-12
    • 2012-08-26
    • 2012-04-15
    • 1970-01-01
    • 2015-06-12
    • 2020-10-18
    • 1970-01-01
    • 2013-06-08
    相关资源
    最近更新 更多