我使用表从两个表中获取所有记录,效果很好。我在每一行的末尾添加了一个按钮,显示 Modal。在此模式中,我想根据每一行 ID 显示数据。
我的代码有一个 while 循环,它显示表中的记录,在那个 while 循环中,我又写了一个 sql 语句,它有一个 while 循环,应该按照行 ID 运行,但它只需要一个 rowId行,并对所有行显示相同的模态
请帮助我,如何克服这些错误,以及我哪里错了..非常感谢你帮助我指导正确的方法
<?php
session_start();
if(!isset($_SESSION)){
header("Location: ../login.php");
exit(); }
include '../header.php';
?>
<!DOCTYPE html>
<html>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Processed Payment
</h1>
<ol class="breadcrumb">
<li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<li><a href="#">Tables</a></li>
<li class="active">Data tables</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-xs-12">
<!-- /.box -->
<div class="box">
<div class="box-header">
<h3 class="box-title"> Processed Payment
</h3>
</div>
<!-- /.box-header -->
<?php
$email1 = $_SESSION['email'];
$Vendor_id="SELECT Vendor_id FROM vendors where email = '$email1' ";
$result=mysqli_query($conn,$Vendor_id);
$row = mysqli_fetch_row($result);
$sql = "SELECT cp.Customer_payment_id, cp.transaction_id,cp.type,co.Customer_order_id,co.shipping_method,co.tracking_id,co.service_name,co.expected_date_of_Delivery,co.payment_status FROM customer_payment cp, customer_order co where cp.Customer_order_Customer_order_id=co.Customer_order_id and co.Vendors_Vendor_id=$row[0]";
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>
<div class="box-body">
<table id="example1" class="table table-bordered table-striped">
<form method="post">
<thead>
<tr>
<th>ID</th>
<th>Transaction ID</th>
<th>Payment Type</th>
<th>Status</th>
<th>Order_ID</th>
<th>Download</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_array($query))
{
$cpid=$row['Customer_payment_id'];
$trd=$row['transaction_id'];
$type=$row['type'];
$pays=$row['payment_status'];
?>
<tr>
<td><?php echo $cpid;?></td>
<td><?php echo $trd;?></td>
<td><?php echo $type;?></td>
<td><?php echo $pays;?></td>
<td><div class="box-body">
<button type="button" name="submit" value=<?php echo $cpid;?> class="btn btn-warning" data-toggle="modal" data-target="#modal-default1">
Order ID</div>
<div class="modal fade" id="modal-default1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title">Default Modal</h4>
</div>
<div class="modal-body">
<div class="body table-responsive">
<table class="table">
<?php
$modal123 = "SELECT co.Customer_order_id, co.shipping_method,co.tracking_id,co.service_name,co.expected_date_of_Delivery FROM customer_payment cp, customer_order co where cp.Customer_order_Customer_order_id=co.Customer_order_id and cp.Customer_payment_id=$cpid";
$modalqry = mysqli_query($conn, $modal123);
if (!$modalqry) {
die ('SQL Error: ' . mysqli_error($conn));
}
while ($row = mysqli_fetch_array($modalqry))
{
$cid=$row['Customer_order_id'];
$sm=$row['shipping_method'];
$tid=$row['tracking_id'];
$sn=$row['service_name'];
$dod=$row['expected_date_of_Delivery'];
?>
<thead>
<tr>
<th>Order Track_ID</th>
<th><?php echo $cid;?></th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Shipping Method</th>
<td><?php echo $sm;?></td>
</tr>
<tr>
<th scope="row">Tracking ID</th>
<td><?php echo $tid;?></td>
</tr>
<tr>
<th scope="row">Service Name</th>
<td><?php echo $sn;?></td>
</tr>
<tr>
<th scope="row">Date of Delivery</th>
<td><?php echo $dod;?></td>
</tr>
</tbody>
<?php } ?>
</table>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default " data-dismiss="modal">Close</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</td>
<td><a href="../uploads/<?php echo $cpid;?>.pdf"><div class="box-body"><button type="button" class="btn btn-block btn-info ">
<!--<a href="#" class="w3-btn w3-black"></a>-->
<span>Download</span>
</button>
</div></a></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Transaction ID</th>
<th>Payment Type</th>
<th>Status</th>
<th>Order_ID</th>
<th>Download</th>
</tr>
</tfoot>
</form>
</table>
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</section>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<?php
include '../footer.php';
?>
</html>
最佳答案
它都显示相同的模态内容,因为你所有的模态都有相同的 ID。所以每次打开模态时,它只显示最后一个模态。要解决此问题,您需要使每个模态的 ID 动态化,或者您可以使用 $cpid 作为模态名称。引用以下代码:
来自:
<div class="box-body">
<button type="button" name="submit" value=<?php echo $cpid;?>
class="btn btn-warning" data-toggle="modal" data-target="#modal-default1"> Order ID
</div>
<div class="modal fade" id="modal-default1">
</div>
改成
<div class="box-body">
<button type="button" name="submit" value=<?php echo $cpid;?>
class="btn btn-warning" data-toggle="modal" data-target="#modal-<?php echo $cpid;?>"> Order ID
</div>
<div class="modal fade" id="modal-<?php echo $cpid;?>">
</div>
关于php - 每行上的模态按钮显示从数据库中获取详细信息的模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47807924/