Page 1 of 1
Pulldown Menu Question
Posted: Thu Apr 15, 2004 6:45 pm
by AliasBDI
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?
Posted: Thu Apr 15, 2004 6:58 pm
by markl999
Yep, it's possible.
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>
So you just need to change $selected to the value from the db, if that makes sense

hmmm
Posted: Thu Apr 15, 2004 10:18 pm
by AliasBDI
the problem is that the pulldown menu is pulled from a database table. I don't know the records. Therefore I cannot determine the variable...
Posted: Fri Apr 16, 2004 6:58 am
by magicrobotmonkey
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!
?>
Posted: Fri Apr 16, 2004 1:13 pm
by Unipus
Here's something similar.
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>";
}
}
You could make it even shorter with a switch statement, but I think it gets a bit unreadable then.
Posted: Wed May 12, 2004 11:39 am
by dave420
Using a database class, it can be achieved remarkably easily:
Code: Select all
<?php
$rows=$db->getRows("select name, userid from user");
foreach ($rows as $i=>$r) {
$rows[$i]["selected"]=($userid==$r["userid"]);
}
?>
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:
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>';
?>
You really should get into templating - it makes things like this a snap

Posted: Wed May 12, 2004 5:23 pm
by pickle
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>";