Well, I will assume your script has some way of figuring out who a user is when they visit this page. Once you are able to do that, you just query and store the data for a user.
So, in your case you may have a table with the following columns for each user:
And a seperate table that contains a list of all possible departments, that may look like this:
When you put the two together, the users deptid column will match exactly one row in the Departments table. This is the item that you want selected. To do that, we can first do something like this to gather the data needed:
<?php
/* gather useful info, connect to db, etc... */
$db = ...;
$id = ...;
/* get all the users data */
$userQuery = "SELECT * FROM users WHERE uid = $id";
$userResult = mysql_query($userQuery,$db);
$userData = mysql_fetch_row($userResult,$db);
list($uid,$uname,$udept) = $userData;
/* get a list of departments */
$deptQuery = "SELECT * FROM Departments";
$deptResult = mysql_query($deptQuery,$db);
while ($deptData = mysql_fetch_row($deptResult,$db))
{
$depts[] = Array("deptid"=>$deptData[0],"deptname"=>$deptData[1]);
}
?>
Now that all the data is together, you can use it to mark off a select box later in the script when doing the display routines:
<!-- html header and form stuff above here -->
<select name="deptid"><?php
foreach($depts as $key=>$data)
{
if ($udept == $data["deptid"])
{
echo("<option value=\"".$data["deptid"]."\" selected>".$data["deptname"]."</option>");
}
else
{
echo("<option value=\"".$data["deptid"]."\">".$data["deptname"]."</option>");
}
}
?></select>
<!-- html footer down here -->
So what happens is the script knows that the user who requested the page has a department ID of, say, "12". So, while it is writing out each <option> group, it checks the corresponding department id for a match, and if found, makes it "selected".