我在 while 循环中有一个表,其中包含模态的编辑按钮。我想要实现的是,当您单击模式按钮“编辑”时,模式中的数据将与正确的教师姓名相对应。所以我试图循环模态和其中的数据。我所做的不起作用,它只显示循环中所有模态的数组中的第一个或最后一个值。
所以表格看起来像这样
| teacher name | Phone | Email | Edit | Delete |
| Mary | 111-111-1111 | mary@mary.com | Edit | Delete |
| Rob | 111-111-1111 | rob@rob.com | Edit | Delete |
| Liz | 111-111-1111 | Liz@Liz.com | Edit | Delete |
当我单击任何编辑时,所有编辑都会显示教师姓名“Mary”。但我想要的是玛丽显示第一次编辑,罗布显示第二次编辑,依此类推......
代码:
<!--TABLES-->
<div id="table" class="table-responsive-sm container">
<table id="feisHistory" class="table table-striped table-bordered responsive table-hover" cellspacing="0" style="width:100%">
<thead>
<tr>
<th id="teacherName" >Teacher Name</th>
<th id="phone">Phone</th>
<th id="email">Email</th>
<th id="edit" data-feild="operate" data-events="operateEvents" data-formatter="operateFormatter" class="d-print-none">Edit</th>
<th id="delete" data-feild="operate" data-events="operateEvents" data-formatter="operateFormatter" class="d-print-none">Delete</th>
</tr>
</thead>
<tbody>
<?php
$school_sql = "SELECT * FROM `Users` WHERE id = '$id'";
$school_res = mysqli_query($con,$school_sql);
$school_row = mysqli_fetch_array($school_res);
$school_name = $school_row["school"];
$sql = "SELECT * FROM `teachers` WHERE school = '$school_name' ORDER BY teacher ASC";
$res = mysqli_query($con,$sql);
if($res==FALSE){
die('there was an error running query [' . $con->error . ']');
}
while($row=mysqli_fetch_array($res)){
$teacherName = $row["teacher"];
$teacherPhone = $row["phone"];
$teacherEmail = $row["email"];
$teacherId = $row["id"];
?>
<tr>
<td><?php echo $teacherName; ?></td>
<td><?php echo $teacherPhone; ?></td>
<td><?php echo $teacherEmail; ?></td>
<td class="d-print-none" style="margin: auto;">
<a href="" data-toggle="modal" data-target=".bd-editprofile-modal-lg" style="cursor: pointer; color: #749800;">Edit</a>
<!---MODAL BUTTON-->
<div class="col-sm-12 content-btn">
<div class="row">
<div class="col-sm-12">
<div class="formModal1">
<div class="modal fade bd-editprofile-modal-lg" id="editProfile" tabindex="-1" role="dialog" aria-labelledby="editProfile" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-success modal-lg" role="document">
<div class="modal-content">
<div class="modal-header" style="color: #464646">
<h5 class="modal-title text-center">Edit Teacher</h5>
</div>
<form id="updateForm" method="post" class="form-horizontal" name="tcol" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" role="form" data-toggle="validator">
<input type="hidden" name="id" value="">
<div class="controls">
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group col-sm-12">
<div class="row">
<div class="col-sm-3">
<label> Teacher Name</label>
</div>
<div class="col-sm-8">
<input id="teacher_name" type="text" name="teacher_name" class="form-control" placeholder="<?php echo $teacherName?>">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- MODAL FOOTER -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save" id="save">Save changes</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="d-print-none" style="margin: auto;">
<a href="delete_teacher?id=<?php echo $teacherId; ?>" style="cursor: pointer; color: #749800;" onclick="return confirm('Are you sure you want to delete this teacher? You will never be able to get this information back.');">Delete</a>
</td>
</tr>
<?php } ?>
</tbody>
</table>
我知道我的代码很草率,请不要批评我太多..我还在学习中。
最佳答案
您可以使用非ajax版本,但您应该避免这种情况,因为它会重写大量代码(取决于记录),通过在while
中为每个元素id分配唯一的id > 循环,也许您可以使用 $teacherId
作为标识符:
while($row=mysqli_fetch_array($res)){
$teacherName = $row["teacher"];
$teacherPhone = $row["phone"];
$teacherEmail = $row["email"];
$teacherId = $row["id"];
?>
<tr>
<td><?php echo $teacherName; ?></td>
<td><?php echo $teacherPhone; ?></td>
<td><?php echo $teacherEmail; ?></td>
<td class="d-print-none" style="margin: auto;">
<a href="" data-toggle="modal" data-target=".bd-editprofile-modal-lg" style="cursor: pointer; color: #749800;">Edit</a>
<!---MODAL BUTTON-->
<div class="col-sm-12 content-btn">
<div class="row">
<div class="col-sm-12">
<div class="formModal1">
<div class="modal fade bd-editprofile-modal-lg" id="editProfile<?=$teacherId?>" tabindex="-1" role="dialog" aria-labelledby="editProfile" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-success modal-lg" role="document">
<div class="modal-content">
<div class="modal-header" style="color: #464646">
<h5 class="modal-title text-center">Edit Teacher</h5>
</div>
<form id="updateForm<?=$teacherId?>" method="post" class="form-horizontal" name="tcol" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" role="form" data-toggle="validator">
<input type="hidden" name="id" value="">
<div class="controls">
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="form-group col-sm-12">
<div class="row">
<div class="col-sm-3">
<label> Teacher Name</label>
</div>
<div class="col-sm-8">
<input id="teacher_name<?=$teacherId?>" type="text" name="teacher_name" class="form-control" placeholder="<?php echo $teacherName?>">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- MODAL FOOTER -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" name="save" id="save<?=$teacherId?>">Save changes</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</td>
<td class="d-print-none" style="margin: auto;">
<a href="delete_teacher?id=<?php echo $teacherId; ?>" style="cursor: pointer; color: #749800;" onclick="return confirm('Are you sure you want to delete this teacher? You will never be able to get this information back.');">Delete</a>
</td>
</tr>
<?php } ?>
关于php - 如何将 while 循环变量添加到引导模式 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55113993/