Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.