Mar1AK
Mar1AK

Reputation: 29

PHP dynamic drop down list

I am trying to make a drop down list which will contain as many options as there are in the database table.

    $qry3 = "SELECT * FROM employees WHERE buss_id_fk = '{$_SESSION ['user_id']}' ";
    $result3 = mysqli_query ( $con, $qry3);

    while ($row4 = mysqli_fetch_assoc($result3)){ 
    echo $row4['emp_id'] . " " . $row4['username'] . '</br>';
    }

$qry3 is my query and it works fine with $row4 and everything shows as intended. Now when I move into my form where my drop down list should exist I made this code

 <!--the list of employees the business has -->
                  <select name="employees" class="form-control">

 <?php while ($row3 = mysqli_fetch_assoc($result3)) {

 echo "<option value='" .$row3['emp_id']. "'>" .$row3['username']. "</option>" ;

                  } ?>

                  </select> <br>

my drop down list shows zero results while it should show some, just like that $row4 did. Checked a similar question where there was a typo in the code, didn't help. Thanks!

Upvotes: 1

Views: 63

Answers (1)

John Conde
John Conde

Reputation: 219794

You need to reset the pointer back to the beginning of the result set if you wish to iterate through the results again:

<?php 
    mysqli_data_seek($result3, 0); // Zero indicates the beginning
    while ($row3 = mysqli_fetch_assoc($result3)) {

See the manual: mysqli_data_seek()

Upvotes: 4

Related Questions