Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

php - How do I fetch data in select option during editing data looped from the database?

I have an issue with my code. I am using a model to update data, but in my form there is select option where by when I need all data to be populated I found only one row populated in it. If I have to populate values in that option by using <?php while ($row = mysqli_fetch_array($query)) {?>. All row populated in that option field but in the list where all row of data needed to be populated, it populates only one.

<?php 
    include('../Tools/connection.php');
    $query = mysqli_query($db,"SELECT abl_atms.atmCode,abl_atms.atmID,abl_atms.branchID, abl_branch.name,abl_branch.bid
                                FROM abl_atms
                                INNER JOIN abl_branch ON abl_branch.bid = abl_atms.branchID");
    while ($row = mysqli_fetch_array($query)) {
      $id = $row['atmID'];
      $id = $row['branchID']
      ?>
        <tr>
          <td><?php echo strtoupper($row['atmID']);?></td>
          <td><?php echo ucwords($row['atmCode']).'- '.strtolower($row['name']);?></td>
          <td><?php echo strtoupper($row['name']);?></td>
          <td class="pull-right">
          <button class="btn-success btn-xs" data-toggle="modal" href="#atm<?php echo $row['atmID'];?>" 
                  data-toggle="tooltip" title="Edit">
          <i class="fa fa-pencil-square-o" aria-hidden="true"></i></button>
          <button class="btn-danger btn-xs" title="Delete" data-toggle="modal"
                href="#delete<?php echo $row['atmID'];?>" data-toggle="tooltip">
                <i class="fa fa-trash-o" aria-hidden="true"></i>
          </button>
          </td>
        </tr>
        
        
        
        <div class="modal fade" id="atm<?php echo $row['atmID'];?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="card" style="border-left: 4px solid coral">
                <div class="card-header">
                  <div class="alert alert-warning" role="alert">
                      Update Atm information
                  </div>
                </div>
                    <div class="card-body">
                        <form class="was-validated" action="abl_atms.php" method="POST">
                            <fieldset class="form-group">
                                <label for="exampleInputEmail1" class="bmd-label-static"><i class="fa fa-barcode" aria-hidden="true"></i> ATM CODE</label>
                                <input type="text" class="form-control" name="atmCode" value="<?php echo ucwords($row['atmCode']);?>" placeholder="Type ATM Identification CODE" required>
                            </fieldset>
                            <input class="form-control" name="atmID" type="hidden" value="<?php echo $row['atmID'];?>">
                            <fieldset class="form-group">
                                <label for="exampleInputEmail1" class="bmd-label-static"><i class="fa fa-snowflake-o" aria-hidden="true"></i> ATM BRANCH</label>
                                <select class="custom-select" required name="branchID">
                                    <option value="">Select Branch Name</option>
                                    <?php
                                        while ($row = mysqli_fetch_array($query)) {
                                      ?>
                                    <option value="<?php echo $row['branchID']; ?>"<?php if ($id == $row['branchID']) 
                                    { echo 'selected="selected"'; } ?>><?php echo $row['name']; ?></option> 
                                    <?php 
                                        }
                                      ?>                        
                                </select>
                            </fieldset>                            
                                <button type="submit" class="btn btn-raised btn-warning btn-block" name="editAtm">
                                  <i class="fa fa-floppy-o" aria-hidden="true"></i> Confirm & Update
                                </button>
                        </form>
                    </div>
                  </div>
                </div>
            </div>
        </div> 
        <?php 
          }
        ?>       
      </tbody>
    </table>
  </div>
  </div>
</div>
question from:https://stackoverflow.com/questions/65950799/how-do-i-fetch-data-in-select-option-during-editing-data-looped-from-the-databas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can store results in an array and then loop over results.

$query = mysqli_query($db,"SELECT abl_atms.atmCode,abl_atms.atmID,abl_atms.branchID,abl_branch.name,abl_branch.bid 
    FROM abl_atms 
    INNER JOIN abl_branch 
    ON abl_branch.bid = abl_atms.branchID");
$results = mysqli_fetch_all(MYSQLI_ASSOC);
foreach($results as $v){
    // Your Code....
    foreach($results as $vi){
        // Your Selection Options...
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...