Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modal Button on each row displays modal which fetch details from database

Tags:

html

php

mysql

I have used table to fetch all records from two tables which is working good. I have added one button at end of each row which displays Modal. In this modal i want to display data based on each row ID.

My code have one while loop which displays records in table,, in that while loop i have written one more sql statement which have a while loop and should run as per row ID, but it is taking only one rowId for both the rows, and displaying same modal to all rows

Please help me out,, how to overcome these mistakes, and where im goin wrong.. Thank u very much for helping me n guiding the correct way Below is the table, on clicking modal i want to display modal as per each row ID

Below is modal which is fetching only one row ID

<?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">&times;</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>
like image 971
rashmi sm Avatar asked Jan 29 '23 00:01

rashmi sm


1 Answers

It all shows the same modal content because all your modal have the same ID. So everytime you open the modal, it shows only the last modal. To fix this you need to make the ID of every modal dynamic or you can use the $cpid as modal name. Refer to the code below:

From:

<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>

Change it to

<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>
like image 126
jun drie Avatar answered Jan 31 '23 13:01

jun drie