newbie8899 Posted December 19, 2007 Share Posted December 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/ Share on other sites More sharing options...
The Little Guy Posted December 19, 2007 Share Posted December 19, 2007 <select> <?php while($row = mysql_fetch_array($sql)){ echo '<option value="'.$row['column'].'"'; if($row['column']== 'banana'){echo ' selected="selected"';} echo '>'.$row['column'].'</option>'; } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/#findComment-418214 Share on other sites More sharing options...
phpSensei Posted December 19, 2007 Share Posted December 19, 2007 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/#findComment-418215 Share on other sites More sharing options...
newbie8899 Posted December 19, 2007 Author Share Posted December 19, 2007 Thanks guy, thanks for ur fast reply. Quote Link to comment https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/#findComment-418243 Share on other sites More sharing options...
mr_mind Posted December 19, 2007 Share Posted December 19, 2007 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/#findComment-418261 Share on other sites More sharing options...
newbie8899 Posted December 24, 2007 Author Share Posted December 24, 2007 thanks guys.. u all really help me a lot. Quote Link to comment https://forums.phpfreaks.com/topic/82273-the-drop-down-menu/#findComment-422165 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.