简体   繁体   English

当单击按钮时,while循环仅显示数据库中的最后一行[以显示模式对话框]

[英]while loop shows only the last row in database when click at button[to display modal dialog]

I am facing a problem in a modal dialog. 我在模式对话框中遇到问题。 when the user click at "File", the modal dialog will appear and will show data that fetch from database. 当用户单击“文件”时,将出现模式对话框,并显示从数据库获取的数据。

<?php echo '<table>'; ?>
              <tr>
                <th>
                Name
              </th>
              <th>
                File
              </th>
            </tr>

$query = "SELECT * FROM table";
              $result = mysqli_query($conn, $query);

              if (mysqli_num_rows($result) != 0) {
                while($row = mysqli_fetch_array($result)) {
                  $stu_file=$row['file_upload'];
                  $ins_file=$row['files_uploads'];
                  $name = $row['name'];
                  echo "<tr>";
                  echo "<td>" .$name. "</td>";
                  echo "<td><a href='#' data-toggle='modal' data-target='#myModal'>File</a></td>";//code this

                  echo "</tr>";
                }
             }else{
                echo "No data has been done yet!!";
             }
             ?>
              <!-- Modal -->
        <div class="modal fade" id="myModal" role="dialog">
          <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Student File</h4>
              </div>
              <div class="modal-body">
                 <?php echo"<pre>".$stu_file."</pre>"?>
              </div>
              <div class="modal-header">
                <h4 class="modal-title">Instructor File</h4>
              </div>
              <div class="modal-body">
                 <?php echo"<pre>".$ins_file."</pre>"?>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              </div>
            </div>

          </div>
        </div>
        <!-- End Modal-->

as you see the code above, when I click at file, the modal dialog display but the $stu_file will show only the last row from database. 如上面的代码所示,当我单击文件时,将显示模式对话框,但$ stu_file将仅显示数据库的最后一行。 When I moved the modal dialog inside the while loop, it shows only the first row from database. 当我在while循环内移动模式对话框时,它仅显示数据库的第一行。 please help to fix this. 请帮助解决此问题。

The variables $stu_file and $ins_file are going to be overwritten with each iteration of your loop. 循环的每次迭代都会覆盖变量$stu_file$ins_file So that by the time you get to the end of the loop and print the modal dialogue, the value of those variables is whatever the last row was in your loop. 这样,当您到达循环的结尾并打印模式对话框时,这些变量的值就等于循环中的最后一行。 Additionally, you only have one modal dialogue so your approach doesn't work here. 此外,您只有一个模式对话,因此您的方法在这里不起作用。

Instead, it might be easier for you to just print the data fetched from your database as a JSON object in <script> tags and use javascript to populate the modal dialogue with the necessary information. 取而代之的是,将来自数据库的数据作为JSON对象打印在<script>标记中,并使用javascript填充必要信息的方式对话框,可能会更容易。

For example you can print out the JSON using something like this... 例如,您可以使用以下方式打印出JSON ...

$data = [];
while($row = mysqli_fetch_array($result)) {
    $data[] = $row;
}
echo "<script> var myData = " . json_encode($data) . "; </script>";

Now you can also populate your links with a data-id attribute to correspond to the given id or PK in your database, like echo "<a href='#' data-toggle='modal' data-target='#myModal' data-id='{$row['id']}'>File</a>" in your loop, and that way you can use that in your modal's .on('show.bs.modal') callback to load the appropriate data from your JSON array. 现在,您还可以使用data-id属性填充链接,以对应数据库中给定的id或PK ,例如echo "<a href='#' data-toggle='modal' data-target='#myModal' data-id='{$row['id']}'>File</a>" ,这样您就可以在模态的.on('show.bs.modal')回调中使用它来加载JSON数组中的适当数据。

See the bootstrap documentation for how to use the modal callback . 有关如何使用模式回调的信息,请参阅引导文档 It should be something like... 应该是...

$('#myModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var dataId = button.data('id') // Extract info from data-* attributes
  // Now you can get the data form your JSON object
  var info = myData[dataId]; // or however you set it up
  var modal = $(this);
  modal.find('.modal-title').text('New message to ' + info);
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM