Jump to content


Photo

dropdown menu question


  • Please log in to reply
3 replies to this topic

#1 marccesar

marccesar
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationThousand Oaks, CA

Posted 09 June 2006 - 05:01 PM

i have a drop down menu which is being built from values in my database..how do i specify which value to select when the page loads? thanks!

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 09 June 2006 - 05:06 PM

You can use Javascript and select it when the page loads....
Or figure out which value you want to replace to select and use str_replace, preg_replace and so on.

Like:

$pattern = 'value="' . $should_be_selected . '"';
str_replace($pattern, $pattern . ' selected="selected"', $subject);

~ D Kuang

#3 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 05:12 PM

I think I've already answered this in another thread, but I can't remember if you posted it or not. Either way:
$sql = mysql_query("SELECT *
FROM `table`
WHERE `something`='1'"); // Do the database query
echo "<select name=\"members\">"; // Start off the drop-down menu
while($row = mysql_fetch_array($sql))
{
echo "<option";
if($row['name'] == 'fyorl')
{
echo " selected>"; // This makes the option selected
}
else
{
echo ">";
}
echo "$row[name]</option>";
}
echo "</select>"; // End the menu

[table]



Don't worry, the printer fairies will sort it out.

#4 marccesar

marccesar
  • New Members
  • Pip
  • Newbie
  • 3 posts
  • LocationThousand Oaks, CA

Posted 09 June 2006 - 05:55 PM

thanks for your help, worked perfectly!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users