Struggling with drop-down menus when they are comparing to two tables. I know the logic, but can't seem to put it to code. At this point, I'm just slapping stuff in to see if it works 'cause I'm just not seeing it.
I have a main admin page that simply displays rows of data/info for a user.
One of the columns for each row is the drop-down menu that would allow me to select a name from the CONTRACTORS table and save it to the user's record. That's the drop-down I can't get to show the currently saved contractor.
The table with the 2 seeded names to select from is CONTRACTORS and the column I want is: first_name. I'm able to pull the names and have them show in the drop-down menu. What I can't see to get working is when the USER has a contractor saved to their record, why the SELECTED doesn't work on the drop down menu.
I have two user records seeded. User 1 has a CONTRACTOR seeded by the name of JIMMY. User 2 has a CONTRACTOR seeded with name TAMMY.
WHAT I WANT TO HAPPEN: If user has a name saved in the contractor column in USERS tbl, it should show that name as selected in the drop-down menu. Otherwise, we should see the names from CONTRACTORS to select a name from
Can someone SHOW me how it should look to work properly?
GO TO 10th ROW FROM THE BOTTOM to see the FOREACH I'm struggling with.
Code: Select all
$query = "SELECT users.username, users.status, users.first_name, users.last_name, users.contractor, users.packet_sent, users.last_login,
users.last_note, users.affiliate, users.start_date, contractors.first_name AS cfirst_name
FROM users
INNER JOIN contractors
ON users.user_id = contractors.user_id
WHERE users.usertype = '6' && users.active_state = '1' ";
}
// need two results; one for each 'while' statement below
$result1 = mysqli_query($con, $query) or die (mysqli_error($con));
$result2 = mysqli_query($con, $query) or die (mysqli_error($con));
// $array = array(); // save this in-case needed, but seems to be working without it. Thought it was needed 'cause array was being used outside of the while statement.
// create the while loop that will check for all contractors that exist and put them in an array
while($row1 = mysqli_fetch_array($result1)) {
$array[] = $row1['cfirst_name'];
}
// create a while loop that will grab everything on admin main and display it
while ($row = mysqli_fetch_array($result2)) {
// convert unix time back to regular date format
$date_conversion = gmdate("m-d-Y", $row['start_date']);
echo
"<td>"; print number_format(($unix_time - $row['last_login']) / 86400.); echo " days ago</td>".
"<td style=\"text-decoration: none;\"><a href=\"mailto:".$row['username']. "\">".$row['last_name'].", ".$row['first_name']."</a></td>";
if ($row['status'] == 'complete') {
echo "<td><font color=\"#FF0000\">".$row['status']."</font></td>";
} else {
echo "<td>".$row['status']."</td>";
}
echo
"<td>".
"<form id=\"assign_contractor\" method=\"post\">".
"<select name=\"assign_contractor\" class=\"dropmenu100\" onChange=\"selectChange2()\" \">".
"<option value=\"\" selected='selected'>Select</option>";
foreach ($array as $val) {
echo "<option value=\"".$val."\">".$val."</option>";
}
echo
"</select>".
"</form>".
"</td>".