Trying to add my modal to my table where I am already looping my data from the database but the modal only gets the first data from the loop, While the table loops perfectly. How do I get the modal to loop appropriately and would like to know in detail what I am missing?
Would a multidimensional array work? or the modal should not be called in the table? Tried the last bit and it did not work also.
<div class="table100">
<table id="p" class="table table-bordered" style="width:100%">
<thead>
<?php
$que = mysqli_query($connection, "SELECT * FROM account WHERE account_no = '$acc_no' && client_id ='$id'");
$q = mysqli_fetch_array($que);
$acc_id = $q['id'];
$result = mysqli_query($connection, "SELECT * FROM account_transaction WHERE (account_no = '$acc_no' && int_id = '$sessint_id') && (transaction_date BETWEEN '$std' AND '$endx') ORDER BY transaction_date ASC");
?>
<tr class="table100-head">
<th class="column1">Transaction-Date</th>
<th class="column2">Value Date</th>
<th class="column3">Reference</th>
<th class="column4">Debits(₦)</th>
<th class="column5">Credits(₦)</th>
<th class="column6">Balance(₦)</th>
<th></th>
</tr>
</thead>
<tbody>
<?php if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$nxtbal = $row["running_balance_derived"];
?>
<tr>
<!-- <td></td> -->
<td class="column1"><?php echo $row["transaction_date"]; ?></td>
<td class="column2"><?php echo $row["created_date"]; ?></td>
<?php
if ($row["transaction_type"] == "") {
$desc = "Deposit";
} else if ($row["transaction_type"] == "") {
$desc = "Withdrawal";
} else if ($row["transaction_type"] == "loan_disbursement") {
$desc = "Loan Disbursment";
} else if ($row["transaction_type"] == "percentage_charge") {
$desc = $row["description"];
} else if ($row["transaction_type"] == "flat_charge") {
$desc = $row["description"];
} else {
$desc = $row["description"];
}
?>
<td class="column3"><?php echo $desc; ?></td>
<td class="column4"><?php echo number_format($row["debit"], 2); ?></td>
<td class="column5"><?php echo number_format($row["credit"], 2); ?></td>
<?php
// $newnext = mysqli_query($connection, "SELECT transaction_date, running_balance_derived, RunningTotal = SUM(running_balance_derived) AS OVER (ORDER BY transaction_date ROWS UNBOUNDED PRECEDING) FROM account_transaction WHERE account_id = '$acc_id' && (int_id = $sessint_id && branch_id = '$branch') && (transaction_date BETWEEN '$std' AND '$endx') ORDER BY transaction_date ASC");
// $mink = mysqli_fetch_array($newnext);
// $qwe = $mink["running_balance_derived"];
?>
<td class="column6"><?php echo number_format($nxtbal, 2); ?></td>
<td>
<a href="" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Edit</a>
<!-- Button trigger modal -->
<!-- POP UP BEGINS -->
<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-header">
<h3 class="modal-title" id="exampleModalLabel">Reverse Transaction</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="../functions/transactions/reversal.php" method="POST">
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">ID: </label>
<input class="form-control" type="number" name="id" placeholder="" readonly value="<?php echo $row["id"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Account_no: </label>
<input class="form-control" type="text" name="account_number" placeholder="" readonly value="<?php echo $row["account_no"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Date: </label>
<input class="form-control" type="date" name="transaction_date" placeholder="" readonly value="<?php echo $row["transaction_date"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Amount:</label>
<input class="form-control" type="number" name="amount" placeholder="" readonly value="<?php echo $row["amount"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Receipt Number</label>
<input class="form-control" type="number" placeholder="" readonly value="<?php echo $row["transaction_id"]; ?>">
</div>
</div>
<button type="button" type="submit" class="btn btn-primary">Reverse</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- POP UP ENDS -->
</td>
</tr>
<?php
}
}
?>
<!-- <th></th> -->
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Modal id will be same for all of your modals so it will trigger only one. Please change put the modal out side the loop and pass the data on an event or change the modal id by auto generated id to make it unique.
As @keerthi Vijay Mentioned the issue I had was with the ids of the modal, So for a code example on how to sort that out.
<td>
<a href="" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal<?php echo $row["id"]; ?>">Edit</a>
<!-- Button trigger modal -->
<!-- POP UP BEGINS -->
<div class="modal fade" id="exampleModal<?php echo $row["id"]; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h3 class="modal-title" id="exampleModalLabel">Reverse Transaction</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="../functions/transactions/reversal.php" method="POST">
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">ID: </label>
<input class="form-control" type="number" name="id" placeholder="" readonly value="<?php echo $row["id"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Account_no: </label>
<input class="form-control" type="text" name="account_number" placeholder="" readonly value="<?php echo $row["account_no"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Date: </label>
<input class="form-control" type="date" name="transaction_date" placeholder="" readonly value="<?php echo $row["transaction_date"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Amount:</label>
<input class="form-control" type="number" name="amount" placeholder="" readonly value="<?php echo $row["amount"]; ?>">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="bmd-label-floating" for="">Receipt Number</label>
<input class="form-control" type="number" placeholder="" readonly value="<?php echo $row["transaction_id"]; ?>">
</div>
</div>
<button type="button" type="submit" class="btn btn-primary">Reverse</button>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- POP UP ENDS -->
</td>
All I did was concatenate the ID I was getting from my db to the modal id and viola you have a dynamic ID at hand. Dropping these just incase you get the same problem and you are a bit confused on how to apply the solution mentioned earlier.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.