I'm trying to populate a dropdown menu by comparing two tables, one has a list of supervisor and employee numbers, the other has employee numbers and names. I need to take the numbers for each supervisor and employee and turn them into employee names for the drop down menu, so basically
TABLE payroll_employeelist
Supervisor Employee
1234 3456
1234 2239
1234 123
2910 338
2910 3901
TABLE payroll_users
number name
3456 John Smith
2239 Mary Jane
123 Joe Brown
etc
Supervisors are identified by a session variable callede $usernumber. What I have so far and is returning one result (just one!) is the following:
if ($loademployees == 1){
echo "<option value=\"base\">---- Employee Name ----</option>";
$query = "SELECT payroll_employeelist.employee, payroll_users.number, payroll_users.name FROM payroll_employeelist WHERE supervisor='$usernumber' LEFT JOIN payroll_users ON payroll_employeelist.employee=payroll_users.number ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<option value=\">" . $row{'name'} . "</option>";
}
echo "</select><br>";
}
Can anyone help with this? I get the feeling I've done something funny with the JOIN. It should look like a list of employee names in the dropdown.
UPDATE:
What I have now is:
if ($loademployees == 1){
echo "<option value=\"base\">---- Employee Name ----</option>";
$query = "SELECT payroll_employeelist.supervisor, payroll_employeelist.employee, payroll_users.number, payroll_users.name
FROM payroll_employeelist
INNER JOIN payroll_users
ON payroll_employeelist.employee = payroll_users.number
WHERE supervisor = '$usernumber' ";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<option value=\">" . $row['name'] . "</option>";
}
echo "</select><br>";
}
This is successfully returning one of the three records in the test data set, just one, the middle record. The $usernumber is generated internally by the way, no injection possible.
LAST UPDATE- SOLVED The problem believe it or not was
echo "</select><br>";
it was echoing that before echoing the results of the while loop so it thought the options list was empty. I can't explain the randomly appearing single employee mind you, but it's working now.