Jump to content

the drop down menu


newbie8899

Recommended Posts

Hi all.. i m not sure shud i post this question to html forum or php as it needs the php to select the value from the db. so i decide to post it here.

 

I have a problem with the <select></select> stuff.  my problem is like this.

 

If i have a set of pull down menu using <select></select> the values in it are:

orange, apple and banana.

 

at the beginning, i will pull out the value from the db.. for example, if the value pull out from the db table is banana. so i want my <select></select> in the form to select the banana instead of keep selecting the 1st item.. how am i going to do this?

 

thanks a lot

Link to comment
https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/
Share on other sites

or

 

<?php


$query   = mysql_query("SELECT * FROM foods");

echo '<select name="select">';

while($row = mysql_fetch_array($query)){

     if($row[item] == "banana"){

     echo '<option value="'.$row['item'].'" selected>banana</option>';

         }
          else 
            {

             echo '<option value="'.$row['item'].'">banana</option>';

                 }
  if($row[item] == "apple"){

     echo '<option value="'.$row['item'].'" selected>apple</option>';

         }
          else 
            {

             echo '<option value="'.$row['item'].'">apple</option>';

                 }
  if($row[item] == "sensei"){

     echo'<option value="'.$row['item'].'" selected>sensei</option>';

         }
          else 
            {

             echo '<option value="'.$row['item'].'">sensei</option>';

                 }

}

echo '</select>';
?>

Link to comment
https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/#findComment-418215
Share on other sites

I'm sorry it took me a while to get through your question. I rephrased it in a

way that normal humans can understand and I fixed your numerous spelling errors.

 

What I finally found that you were asking:

"I have a select menu. The values in it are orange, apple and banana. The value

for each of the options are returned from the database. For example, if the

first item in the menu is by default apple and the value from the database is

banana I want the current option to be banana instead of apple."

 

Here is my answer:

 

<?php
print '<select name=fruit>';
print '<option value=banana> apple </option>';
$fruit_query = mysql_query("SELECT * FROM fruits");
while($fruit_array = mysql_fetch_array($fruit_query)) {
	print '<option value=' . $fruit_array['fruit_name'] . ';
	if($fruit_array['fruit_name']=='banana') {
		print 'SELECTED';
	}
	print '>' . $fruit_array['fruit_name'] . '</option>';
}
print '</select>';
?>

Link to comment
https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/#findComment-418261
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.