'Didn't get the ID from database based on row table

Why I can't get the ID from a row in table when I click Pending

Here is the code:

<!-- ############################################################################################################################################################################################################ -->
<!-- TABLE BUSINESS -->
<div class="row" id="TblBiz" style="display: none;">
            <div class="col" style="height: 71px;">
                <h1 style="font-family: Roboto, sans-serif; font-weight: 300;">Business</h1>
            </div>
                <div class="col-md-8 offset-md-2">
                <div class="panel" id="main">
                    <div class="container-fluid container-bg">
                        <div class="table-responsive">
                            <table id="myTable4" class="table table-hover table-dark" style="font-size:11px; margin-top:6px;">
                                <thead style="font-size: 18px;">
                                    <th>#</th>
                                    <th>Business ID</th>
                                    <th>Name</th>
                                    <th>Owner</th>
                                    <th>View</th>
                                    <th>Approval</th>
                                </thead>
                                <tbody>
                                    <?php
                                    $sql = "SELECT * FROM mechanic_business";
                                    $result = mysqli_query($connect, $sql);
                                    
                                        if(mysqli_num_rows($result) > 0){
                                    
                                        $i = 1;
                                    
                                        while ($row = mysqli_fetch_assoc($result)) {
                                            $biz_ID = $row['businessID'];
                                            $mech_ID = $row['mechID'];
                                            $mech_name = $row['mech_name'];
                                            $bizName = $row['business_name'];
                                            $bizApprove = $row['business_approval_status'];
                                            $bizStatus = $row['business_status'];
                                            // $dateJoin = $row['DATE_REGISTERED'];
                                            // $dateFormated = date("d M Y", strtotime($dateJoin));

                                            // Color class in PHP color(Pending, Approved, Banned)
                                            $color = ($bizApprove === 'Pending') ? '#f0ad4e' : (($bizApprove === 'Approved') ? '#28a745' : '#dc3545');

                                                echo "<tr style='font-size:18px;'>";
                                                echo "<td class='align-middle'>" . $i; $i++ . "</td>";
                                                echo "<td class='align-middle'>" . $biz_ID . "</td>";
                                                echo "<td class='align-middle'>". $bizName ."</td>";
                                                echo "<td class='align-middle'>". $mech_name ."</td>";
                                                echo "<td class='align-middle'><input type='button' name='view' value='view' id='". $biz_ID ."' class='btn btn-info btn-xs view_biz'></td>";
                                                echo "<td class='align-middle'><a role='button' class='approval-bton' id='". $biz_ID ."' style='color:". $color ."';>". $bizApprove ."</a></td>"; 
                                                echo "</tr>";
                                            }
                                        } else {
                                            echo "<tr>";
                                            echo "<td colspan='7' class='table-active align-middle'>No Record in Database</td>";
                                            echo "</tr>";
                                        }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
              </div>
            </div>
<!-- ############################################################################################################################################################################################################ -->

<!-- ############################################################################################################################################ -->
<!-- UPDATE APPROVE STATUS MODAL -->
    <div class="modal fade" id="approveModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel"> Update Business Approval Status </h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>

                <form action="" method="post" enctype="multipart/form-data">

                    <div class="modal-body">

                        <input type="text" name="businessID" id="" value="<?php echo $biz_ID; ?>">

                        <div class="form-group">
                            <div>
                                <small id="photoHelp" class="form-text text-muted"><span style="color: #FF0000;">*</span><em>320px x 320px is ideal dimension, Format: jpg, png, jpeg, and PDF only</em></small>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" name="updateApprove" class="btn btn-primary">Save Change</button>
                    </div>
                </form>

            </div>
        </div>
    </div>
<!-- ############################################################################################################################################ -->

When I clicked a Pending from each row it suppose to get their ID based on the database but when I clicked from each row they display the same ID as shown picture in the link below.

https://drive.google.com/file/d/1IcfDUs3Q8Wl5vVO3KF793M5noTukLrCL/view?usp=sharing

They both show ORVA01BIZ-00000002 but in database first row is ORVA01BIZ-00000001 and ORVA01BIZ-00000002 is the second row and goes on.

php


Solution 1:[1]

There is very simple logic.

  1. first, your model is not in loop so you can't pass $biz_ID in the model if you want to get each id from the database.

When the loop ends then it will only print the last row biz_id in the model.

The solution is :-

Changes you have to do in your code

You have to do one thing if you want to get each id

This is your second td :-

  1. echo "<td class='align-middle bizzid'>" . $biz_ID . "</td>";

In your model you have to paste this : -

  1. <input type="text" name="businessID" id="businessID" value="">-

This jquery code can help you to get desired result.

 var bizid = $(this).closest('tr').find('.bizzid').text();
 $('#businessID').val(bizid);
 $('#approveModal').modal('show');
 });'

Solution 2:[2]

Okay, got it

I did what you asked @Gurpreet Kait it really workss.

I put the jquery code inside the initialize modal bootstrap

// Show Modal 1
$(document).ready(function () {
  $('.approval-bton').on('click', function() {
      var bizid = $(this).closest('tr').find('.bizzid').text();
      $('#businessID').val(bizid);
      $('#approveModal').modal('show');

  });

Thank you very much @Gurpreet Kait for your help. });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gurpreet Kait
Solution 2