我是 php 新手,我正在嘗試制作一個戲劇學校管理應用程式。我有一個部分,我創建了一個包含專案的所有學生的表,并且我希望表中的每一行都有一個按鈕,可以打開一個包含所有專案周的模式,以及一個復選框來檢查那些支付每周費用的人。我試圖讓它與下面的代碼一起作業,但只有最后一條記錄打開了模式。預覽記錄不做任何事情。如果我在表中添加另一條記錄,則只有新記錄會打開模式。請幫忙!
我的代碼:
<tbody id="firstClass">
<?php
if( mysqli_num_rows($result) > 0){
// we have data
// output the data
while( $row = mysqli_fetch_assoc($result) ) {
$id = $row["id"];
/* print_r($id); */
echo "<tr>";
echo "<td>" . $row['id'] . "</td><td>" . $row['firstname'] . "</td><td>" . $row['lastname'] . "</td><td>" . $row['Age'] . "</td><td>" . $row['email'] . "</td><td>" . $row['phone'] . "</td><td>" . $row['sign_date'] ."</td>";
echo '<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-' . $id . '">
<i ></i>
</button></td>';
echo '<td><a href="edit_student.php?id=' . $row['id'] .'" type="button" >
<i ></i>
</a></td>';
echo '<td><a href="delete_student.php?id=' . $row['id'] .'" type="button" >
<i ></i>
</a></td>';
echo "</tr>";
}
} else {// if no entries
echo "<div class='alert alert-warning'>Δεν ?χετε εγγραφ??!</div>";
}
// mysqli_close($conn);
?>
<tr>
<td colspan="11"><div class="text-center"><a href="addStudent.php" type="button" class="btn btn-sm btn-success"><i class="fas fa-plus"></i> Προσθ?κη εγγραφ??</a></div></td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="payments-<?php echo $id; ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Πληρωμ?</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<!-- <?php echo $id ;?> -->
<div class="table-responsive">
<table class="table" id='myTable'>
<thead>
<tr>
<th class="table-cell text-center" scope="col">Week 1</th>
<th class="table-cell text-center" scope="col">Week 2</th>
<th class="table-cell text-center" scope="col">Week 3</th>
<th class="table-cell text-center" scope="col">Week 4</th>
<th class="table-cell text-center" scope="col">Week 5</th>
<th class="table-cell text-center" scope="col">Week 6</th>
<th class="table-cell text-center" scope="col">Week 7</th>
<th class="table-cell text-center" scope="col">Week 8</th>
</tr>
</thead>
<tbody>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
<td class="text-center"><input type="checkbox"></td>
</tbody>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<?php
include('includes/footer.php');
?>
uj5u.com熱心網友回復:
您還必須將模態寫入用于獲取資料的 WHILE 回圈,以便為每一行添加一個單獨的模態。否則只有最后一個 id 會找到模態。像這樣格式化你的代碼:
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
?>
<tr>
<td><?= $id ?></td> //<?= ?> is the short form of <?php echo '' ?>
.................
</tr>
<!-- Modal -->
<div class="modal fade" id="payments-<?php echo $id; ?>" tabindex="-1"
aria-labelledby="exampleModalLabel" aria-hidden="true">
..................
</div>
<?php }
} else { ?>
.............
<?php } ?>
uj5u.com熱心網友回復:
我剛剛瀏覽了你的問題和你的代碼。據我了解,您希望您的模式顯示用戶單擊的特定學生付款記錄的詳細資訊。
不幸的是,您使用了一個回圈,在該回圈中定義了一個變數 $id 并且每次回圈迭代時 $id 的值都會改變。因此,最后,$id 保存了資料庫中最后一條記錄的值。
根據您的代碼,生成的 HTML 結構將是這樣的(考慮到五個學生的記錄):
<tbody>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-1">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-2">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-3">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-4">xyz</button></td>
</tr>
<tr>
<td><button type="button" data-bs-toggle="modal" data-bs-target="#payments-5">xyz</button></td>
</tr>
</tbody>
<div class="modal fade" id="payments-5">
.....
</div>
這里 modal 將 id 設為 5 只是因為 $id 將保存最后一條記錄的值。由于表格最后一行的 id 與模態的 id 匹配,因此模態僅從最后一行打開。
所以,這就是代碼的問題。希望您已經了解您的代碼出了什么問題。現在讓我們來解決這個問題。
為此,我建議您使用 jquery 打開模式。您將在其中使用類選擇器打開模態,并使用資料屬性在模態中傳遞 student_id。
分享一個片段以獲得更好的幫助。
$('.open-modal').on('click', function() {
var student_id = $(this).data('student_id');
$('#student_id').val(student_id);
$('.modal').modal('toggle');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<table>
<tbody>
<tr>
<td><button type="button" class="open-modal" data-student_id='1'>Student 1</button></td>
</tr>
<tr>
<td><button type="button" class="open-modal" data-student_id='2'>Student 2</button></td>
</tr>
<tr>
<td><button type="button" class="open-modal" data-student_id='3'>Student 3</button></td>
</tr>
</tbody>
</table>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body">
<form>
<!–– form to be used for saving the data with student id as hidden input -->
Student ID: <input type="text" id="student_id">
</form>
</div>
</div>
</div>
</div>
這將使您只為 n 名學生創建一個模式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/410636.html
標籤:
