steelerman99 Posted July 27, 2007 Share Posted July 27, 2007 Hello! I have a database with a couple of fields that only have a certain number of options. I have a page where a user selects 1 record and updates it. When i query the database and retrieve the data in that particular field, i want that value to be automatically selected in a drop down menu(since there are a finite number of options and this will prevent the user from putting in bad input which would screw up searching). So if i have a select statement: <select name = "fruit"> <option value = "apple">Apple</option> <option value = "banana">Banana</option> <option value = "orange">Orange</option> </select> and the value pulled out of the database on the query is either "orange" or "banana", how do i get that selected as the default value in my drop down menu when the page loads? Thanks for the help! If you need clarification on what i'm trying to do, just let me know. Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted July 27, 2007 Share Posted July 27, 2007 I had that same problem a few months ago and Barand solved it for me here: http://www.phpfreaks.com/forums/index.php/topic,132026.msg554878.html#msg554878 Hopefully, it will help you too. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 27, 2007 Share Posted July 27, 2007 <?php $fr='banana'; $result=mysql_query('SELECT fruit FROM TABLENAME WHERE name = banana'); echo '<select name = "fruit">'; while($value=mysql_fetch_assoc($result)) { $selected = ($value['fruit']==$fr)?'selected':''; echo '<option value = "apple" '.$selected.'>'.$value['fruit'].'</option>'; } echo '</select>'; ?> i look at the link it seems the same with this but i have code Quote Link to comment Share on other sites More sharing options...
steelerman99 Posted July 27, 2007 Author Share Posted July 27, 2007 could you explain what exactly is going on in your while loop (sorry, i'm not too too advanced with php syntax)? specifically the '?' , ':', and '.' put in odd places- what does that do? are you dynamically building the select statement from a query? i don't want to put the whole query row into a drop down. just one column in the database (let's call it the fruit column), has about 8 choices only (but we'll use 3 in this example). the other columns have nothing to do with fruit. that value is put into a variable say $myfruit, which is a string (lets say $myfruit = "orange" on this particular query of the DB). i just want whatever $myfruit is to be automatically selected in a drop down menu that consists only of "apple", "orange", and "banana" Thanks again guys!! Quote Link to comment Share on other sites More sharing options...
stephenk Posted July 27, 2007 Share Posted July 27, 2007 I had this problem recently. It gets really messy, but here is my solution ... <?php // $filter_age is the value taken from the database, instead of age ranges, you have fruits. <select name="filter_age"> <option value="-">All</option> <option value="2-5"<?php if($filter_age == "2-5") {echo ' selected = "selected"';}?>>2-5</option> <option value="6-12"<?php if($filter_age == "6-12") {echo ' selected = "selected"';}?>>6-12</option> <option value="13-15"<?php if($filter_age == "13-15") {echo ' selected = "selected"';}?>>13-15</option> <option value="16-25"<?php if($filter_age == "16-25") {echo ' selected = "selected"';}?>>16-25</option> <option value="26-39"<?php if($filter_age == "26-39") {echo ' selected = "selected"';}?>>26-39</option> <option value="40-59"<?php if($filter_age == "40-59") {echo ' selected = "selected"';}?>>40-59</option> <option value="60-100"<?php if($filter_age == "60-100") {echo ' selected = "selected"';}?>>60+</option> </select> ?> I built this up quite gradually, but I realised after writiting this code that a much neater solution would be to place all the ages (fruits) in an array, then loop through the array and check if the value from the array matches the value from the database. If so, echo "selected = 'selected'. Stephen Quote Link to comment Share on other sites More sharing options...
Psycho Posted July 27, 2007 Share Posted July 27, 2007 could you explain what exactly is going on in your while loop (sorry, i'm not too too advanced with php syntax)? specifically the '?' , ':', and '.' put in odd places- what does that do? are you dynamically building the select statement from a query? i don't want to put the whole query row into a drop down. just one column in the database (let's call it the fruit column), has about 8 choices only (but we'll use 3 in this example). the other columns have nothing to do with fruit. that value is put into a variable say $myfruit, which is a string (lets say $myfruit = "orange" on this particular query of the DB). i just want whatever $myfruit is to be automatically selected in a drop down menu that consists only of "apple", "orange", and "banana" To answer your first question, those line with ? : in "odd" places are shorthand for an if/else statemetn (ther is a technical name, but I can never remember it). $variable = (condition) ? TrueResult : FalseResult ; As to your second question, the code is putting the values extracted from the query into the select list not the actual query. But, it appears you are doing it the other way around, you have a fixed list and will be querying for the selected value from the database. Int hat case, try this: <?php $result=mysql_query("SELECT fruit FROM TABLENAME WHERE user = 'userid"); $record = mysql_fetch_assoc($result); $selectedFruit = $record['fruit']; $fruitList = array ('Apple', 'Banana', 'Orange'); echo '<select name = "fruit">'; foreach ($fruitList as $fruit) { $selected = ($fruit==$selectedFruit)?'selected':''; echo "<option value=\"$fruit\" $selected>$fruit</option>"; } echo '</select>'; ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted July 27, 2007 Share Posted July 27, 2007 (ther is a technical name, but I can never remember it). $variable = (condition) ? TrueResult : FalseResult ; Ternary operator. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2007 Share Posted July 27, 2007 ...(since there are a finite number of options and this will prevent the user from putting in bad input which would screw up searching).... Im not sure if this has been mentioned yet or not, as i didn't read through all the replies thoroughly , but you shouldn't rely on any sort of HTML or javascript methods of checking user input. What if someone were to copy your page, and modify the source of your form to allow them to send data you did not want them to send? You should always check all user input on the server side. Quote Link to comment Share on other sites More sharing options...
steelerman99 Posted July 30, 2007 Author Share Posted July 30, 2007 Thanks again guys. Also Ginger, i'm not very familiar with php security and what not. How would you go about checking input on the server-side? Quote Link to comment 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.