On a database where you can edit an employees information I need to have a drop down box of all the departments, but the correct department for that employee must be selected. My code half works ... I get the drop down box, but it always shows the last department instead of the department the employee is in.
Can someone show where I have gone wrong??
My code for the drop down -
```php
<select name="deptid">
<?php foreach($results as $row)
{
//set department name the employee is in
if(isset($empid))
{
if ($empid[0]->deptid==$row->deptid)
{
echo '<option value ="'.$row->deptid.'selected="selected">'.$row->deptid."-".$row->department.'</option>';
continue;
}
}
echo '<option value ="'.$row->deptid.'selected="selected">'.$row->deptid."-".$row->department.'</option>';
}?>
</select>
I’m gonna go with… “Because you used the selected = “selected” option for both the If and Else portions of the code, making EVERYTHING ‘selected’” (and thus, the last thing added to the box gets ‘selected’ at the end of the chain, making it display.)
Take the selected out of the second echo.
Thanks for your reply. I removed the second “select”, but now just stays with the first department only, no matter which employee I select.
echo '<option value ="'.$row->deptid.'">'.$row->deptid."-".$row->department.'</option>';
Any ideas???
The HTML inside the if statement is invalid. You need to close the value attribute with a double quote than space.
if ($empid[0]->deptid==$row->deptid)
{
echo '<option value ="'.$row->deptid.'" selected="selected">'.$row->deptid."-".$row->department.'</option>';
continue;
}
thanks for that, I didn’t notice it. I think you look at your code for too long and see nothing!!
Fixed the error, but still not working. My altered code -
<select name="deptid">
<?php foreach($results as $row)
{
//set department name the employee is in
if(isset($empid))
{
if ($empid[0]->deptid==$row->deptid)
{
echo '<option value ="'.$row->deptid.'" selected="selected">'.$row->deptid."-".$row->department.'</option>';
continue;
}
}
echo '<option value ="'.$row->deptid.'">'.$row->deptid."-".$row->department.'</option>';
}
?>
</select>
What is $empid ? When should it be set? When not?
I cannot find any declarion of $empid in your code, and without it I’m rather clueless what you want with it.
$empid is the employees id. The original table has the Employee ID , Employee First Name, Employee Last Name, Department. When you click to edit that employee’s information, you are sent to an edit page where the employee’s first name, last name and department are in the text boxes. I require the department box to be a drop down assuming they need to change departments. I am trying to get the drop down to start at the current position the employee is currently situated.
Hope I have made sense!!
Yes, that makes sense.
Can you do
var_dump($empid);
and post the output so we can see what it looks like?
You’re right $empid hasn’t been passed to the new page, only the employee first name, last name and department. Can I use empfirstname and emplastname to distinguish the employee or do I need to take empid over? Can empid be hidden, as I don’t want that to be allowed to be altered?
thanks
Simply omit empid from the UPDATE query and it can’t be altered.
(omit it from the SET or VALUES clause, not from the WHERE clause of course :))