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

Time:04-27

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

Here is the code:

<!-- ############################################################################################################################################################################################################ -->
<!-- TABLE BUSINESS -->
<div  id="TblBiz" style="display: none;">
            <div  style="height: 71px;">
                <h1 style="font-family: Roboto, sans-serif; font-weight: 300;">Business</h1>
            </div>
                <div >
                <div  id="main">
                    <div >
                        <div >
                            <table id="myTable4"  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  id="approveModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div  role="document">
            <div >
                <div >
                    <h5  id="exampleModalLabel"> Update Business Approval Status </h5>
                    <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
                </div>

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

                    <div >

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

                        <div >
                            <div>
                                <small id="photoHelp" ><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 >
                        <button type="button"  data-bs-dismiss="modal">Close</button>
                        <button type="submit" name="updateApprove" >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.

CodePudding user response:

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');
 });'

CodePudding user response:

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. });

  •  Tags:  
  • php
  • Related