Search code examples
phpmysqlselecthtml-selectdropdown

PHP Select dropdown Option value array


I'm trying to make my select dropdown filter on results by row if that makes any sense!

Essentially I have a table and there's 5 rows in it, each with different date - each row has a dropdown with a list of users that are available to work on that date. I have all the users showing correctly and I have it where it filters a user that can't work that day - the only issue is that it only seems to do it for the first result.

So for example;

User A can't work 10/06/2019 & 11/06/2019 - User A won't show in the dropdown for row dated 10/06/2019, but will show in row dated 11/06/2019.

User B can work on all dates on the table so will show in all dropdowns.

I've tried modifying my array and my query, tried using a counter too but not getting anywhere!

     if ($available_date == $stk_date) {
       $query = "SELECT * FROM user_master WHERE id NOT IN (SELECT UM.id FROM user_master UM JOIN bookings B ON B.id_item = UM.id JOIN stocktakes S ON B.the_date = S.stk_date)";
         $result = mysqli_query($connect, $query);
         //$row = mysqli_fetch_array($result);
         if ($result) {
           while ($row = mysqli_fetch_array($result)){
             echo "<option value=$row[first_name]>$row[first_name]  $row[last_name]</option>'";
           }
           }
     }
     else {
       $query = "SELECT * FROM user_master";
         $result = mysqli_query($connect, $query);
         //$row = mysqli_fetch_array($result);
         if ($result) {
           while ($row = mysqli_fetch_array($result)){
             echo "<option value=$row[first_name]>$row[first_name]  $row[last_name]</option>'";
           }
           }
     }

      echo "</select></td>";

*For some reason my code isn't including my first echo, it's just the id name of the select which is supervisor_id_1

Any ideas on where I'm going wrong?

Update:

Removed update as it's a different question.


Solution

  • In both your IF and ELSE you have a

    $row = mysqli_fetch_array($result);
    

    that is reading the first row from your resultset, but you are not using in your output. Just remove those 2 lines, see code below for annotations

    if ($available_date == $stk_date) {
        $query = "SELECT * FROM user_master 
                    WHERE id NOT IN (
                                    SELECT UM.id 
                                    FROM user_master UM 
                                    JOIN bookings B ON B.id_item = UM.id 
                                    JOIN stocktakes S ON B.the_date = S.stk_date)";
        $result = mysqli_query($connect, $query);
    
        // remove, its just throwing your first result away 
        //$row = mysqli_fetch_array($result);
        if ($result) {
            while ($row = mysqli_fetch_array($result)){
                echo "<option value=$row[first_name]>$row[first_name]  $row[last_name]</option>'";
            }
        }
    } else {
        $query = "SELECT * FROM user_master";
        $result = mysqli_query($connect, $query);
    
        // remove, its just throwing your first result away 
        //$row = mysqli_fetch_array($result);
        if ($result) {
            while ($row = mysqli_fetch_array($result)){
                echo "<option value=$row[first_name]>$row[first_name]  $row[last_name]</option>'";
            }
        }
    }
    
    echo "</select></td>";