foggy839_33
foggy839_33

Reputation: 53

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.

Upvotes: 0

Views: 895

Answers (3)

khuesmann
khuesmann

Reputation: 198

You need quotation marks for the value tag:

value="$var"

All in all there are some quotes at the end of the echo.

Upvotes: 0

Ankit Bhogre
Ankit Bhogre

Reputation: 11

You need to combine two string or concatenate variable, without combine string its always return last echo value. For example : $ex='A1'; $ex.='B1'; $ex.='C2';

Result - A1B1C2

Your Question Answer is :

$string = "<td><select>";
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);
     if ($result) {
       while ($row = mysqli_fetch_array($result)){
         $string.="<option value=".$row['first_name'].">".$row['first_name']." ".$row['last_name']."</option>'";
       }
     }
 }else{
   $query = "SELECT * FROM user_master";
     $result = mysqli_query($connect, $query);
     if ($result) {
       while ($row = mysqli_fetch_array($result)){
         $string.="<option value=".$row['first_name'].">".$row['first_name']." ".$row['last_name']."</option>'";
       }
     }
 }
$string.="</select></td>";

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94642

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>";

Upvotes: 1

Related Questions