如何在动态数据的while循环中使用Modal

时间:2015-10-18 13:14:18

标签: php html while-loop modal-dialog

代码工作正常,但输出不可接受。它是在代码中给出的。代码为每个模态但输出斜杠提供有效数据。

提前感谢您的帮助。

        cout<<"base"<<endl;
        this.f();

The output is received as given

1 个答案:

答案 0 :(得分:2)

使用模态内部循环的方法是错误的,假设您有数千条记录,模态HTML在循环内重复1000次。

  1. 从循环内移除模态并将其放在循环外。
  2. 所以PHP将作为参考,我将页面命名为 Main.php

    <tbody class="table-bordered">
    <tr>
    <?php
    if($result) {
    $i=1;
    while($row = $result->fetch_assoc()) {
    $sql12="select * from users inner join bank_details on users.userid=bank_details.userid  where users.userid='".$row['rec_id']."'";
    $result12=mysqli_query($con,$sql12);
    $row1 = $result12->fetch_assoc();
        echo "<td>".$i."</td><td>".$row['userid']."</td><td>".$row['rec_id']."</td><td>".$row['amount']."</td><td>".$row['details']."</td><td>".$row['date1']."</td>";
            if($uid == $row['rec_id']) {
                echo "<td><button class='btn btn-warning' disabled data-toggle='modal' data-target='#myModal".$i."'>View</button></td>";
            } else {
                echo "<td><button class='btn btn-success' data-toggle='modal' data-target='#myModals".$i."'>View</button></td>";   
            }
            if($uid == $row['userid']){
                if($row['status']=="pending") {
                    echo "<td><button class='btn btn-info' disabled id='sndr'>Pending to Accept</button></td>";
                } else if($row['status']=="accepted") {
                    echo "<td><button class='btn btn-success' disabled id='sndr'>Request Accepted</button></td>";
                } else {
                    echo "<td><button class='btn btn-danger' disabled id='sndr'>Request Rejected</button></td>";
                }
            } else {
                if($row['status']=="pending" ) {
    ?>
        <td>
            <form role="form">
            <div class="form-group">
                <select class="form-control" id="sel1">
                    <option>Select</option>
                    <option value="accepted" onclick="aprvs(<?php echo $row['id']; ?>, this.value);"><span class="btn btn-success"> Accept</span></option>
                    <option value="rejected" onclick="aprvs(<?php echo $row['id']; ?>, this.value);"><span class="btn btn-danger">Reject</span></option>
                </select>
            </div>
            </form>
        </td>
    <?php 
                } else if($row['status']=="accepted") {
                    echo "<td><button class='btn btn-success' disabled id='sndr'>Request Accepted</button></td>";
                }else{
                    echo "<td><button class='btn btn-danger' disabled id='sndr'>Request Rejected</button></td>";
                }
            }
    ?>
    <?php 
        $i++; } 
        } else {
            echo "<td>No Notifications to show</td>";
        }
    ?>
    </tr>
    </tbody>
    </table>
    <?php } ?>
    

    在模态调用按钮中进行以下更改,添加href='info.php?id=".$i."'并从".$i."中删除data-target='#myModal".$i."'

    echo "<td><button class='btn btn-warning' disabled data-toggle='modal' data-target='#myModal' href='info.php?id=".$i."'>View</button></td>";
    
    echo "<td><button class='btn btn-success' data-toggle='modal' data-target='#myModal' href='info.php?id=".$i."'>View</button></td>";
    

    现在将以下模态HTML放在Main.php的循环之外,并将id='<?php echo "myModals".$i.""; ?>'替换为模式标识id='myModal'

    <div class='modal fade' id="myModal" role='dialog' tabindex="-1" aria-hidden="true">
        <div class="modal-dialog" >
        <!-- Modal content-->
            <div class="modal-content">
                //Content Will Load Here
            </div>
        </div>
    </div>
    

    现在创建一个新的PHP文件 info.php 例如

    <?php
    //Database Connection
    $id = $_GET['id'];
    //Run Query to fatch data from database against `$id`
    ?>
    <div class="modal-header" style="background-color:green;padding:10px;">
        <button type="button" class="close" data-dismiss="modal" >&times;</button>
        <center><h4 class="modal-title" align="center" style="color:white;">User Details</h4></center>
    </div>
    <div class="modal-body" style="padding:10px;">
        <table class='table table-striped'>
            <thead>
                <tr><td><h4 style='color:blue;'></b>User Details:</b></h4></td><td></td></tr>
            </thead>
            <tbody>
                <tr><td><b>User Id:</b></td><td><?php echo $row['userid']; ?></td></tr>
                <tr><td><b>Name:</b></td><td><?php echo $row['name']; ?></td></tr>
                <tr><td><b>Father Name:</b></td><td><?php echo $row['fname']; ?></td></tr>
            </tbody>
        </table> 
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
    

    和最后,将以下代码添加到 Main.php 页面,它将从模态中删除旧数据并针对新id

    加载新数据
    $(document).ready(function() {
        $('#myModal').on('hide.bs.modal', function () {
            $(this).removeData('bs.modal');
        });
    });
    

    表结构有大量不必要的<td><tr>标记,有些打开,有些错误导致问题如快照所示,无法覆盖所有这些,请检查此{{3}会让你知道你做错了什么和哪里。