Pulldown Menu Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

Pulldown Menu Question

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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 ;)
AliasBDI
Forum Contributor
Posts: 286
Joined: Fri Nov 15, 2002 10:35 am
Location: Spring, TX, USA

hmmm

Post 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...
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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!

?>
Unipus
Forum Contributor
Posts: 409
Joined: Tue Aug 26, 2003 2:06 pm
Location: Los Angeles, CA

Post 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.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post 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 :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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>";
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply