I have a form with pulldown menus which are filled with query record results. This form will be to update a user account which already exists. So the selections in the pulldown menu need to already be selected on the existing record's selection. But show all selections in case it should be changed.
I have the pulldown menus showing the query results fine, they are just not already preselected according to the users record. Any ideas? Is this even possible?
Pulldown Menu Question
Moderator: General Moderators
Yep, it's possible.
Here's a simple example.
So you just need to change $selected to the value from the db, if that makes sense 
Here's a simple example.
Code: Select all
<?php
$selected = 'two';
?>
<select name="test">
<option value="one"<?php if($selected=='one') echo ' selected' ?>>one</option>
<option value="two"<?php if($selected=='two') echo ' selected' ?>>two</option>
<option value="three"<?php if($selected=='three') echo ' selected' ?>>three</option>
</select>-
magicrobotmonkey
- Forum Regular
- Posts: 888
- Joined: Sun Mar 21, 2004 1:09 pm
- Location: Cambridge, MA
So you just need to change $selected to the value from the db, if that makes sense
Code: Select all
<?php
$selected = $result['selected'];
//where result is the array pulled from the DB and 'selected' is the column you want diplayed!
?>Here's something similar.
You could make it even shorter with a switch statement, but I think it gets a bit unreadable then.
Code: Select all
// use whatever mechanism to set 'selected' here
$selected = 'test'
while ($mysql_result = mysql_fetch_array($query_result))
{
if ($selected == $mysql_result["select_name"])
{
echo "<option value="" . $select_name . "" selected>" . $key_list . "</option>";
}
else
{
echo "<option value="" . $select_name . "">" . $key_list . "</option>";
}
}Using a database class, it can be achieved remarkably easily:
which will set the currently-selected-user (who's ID is "$userid") to selected. You can then pass the array on to your templating engine, or just read it out to generate the table:
You really should get into templating - it makes things like this a snap 
Code: Select all
<?php
$rows=$db->getRows("select name, userid from user");
foreach ($rows as $i=>$r) {
$rows[$i]["selected"]=($userid==$r["userid"]);
}
?>Code: Select all
<?php
echo '<select name="user">';
foreach ($rows as $r) {
echo '<option value="'.$r["userid"].'" '.( $r["selected"]==true ? "selected" : "" ).'>'.$r["name"].'</option>';
}
echo '</select>';
?>So.. the users selections are pulled from the db, but all the items that are in the pulldown are arbitrary? If so, this should work:
Code: Select all
//suppose you have 2 arrays, 1 with the user selections
//($user_selections), 1 with all the options ($options).
echo "<select name = 'ex_select'>";
foreach($options as $index=>$option)
{
$selected = (in_array($option,$user_selections)) ? "selected" : '';
echo <<<OPTION
<option value = '$option' $selected>$option
OPTION;
}
echo "</select>";Real programmers don't comment their code. If it was hard to write, it should be hard to understand.