Jump to content

Dynamically selecting an option in a drop down menu


steelerman99

Recommended Posts

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.

 

Link to comment
Share on other sites

<?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

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

...(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.

Link to comment
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.