Re: Forms

From: Date: Mon, 21 Mar 2005 16:24:26 +0000
Subject: Re: Forms
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-211199@lists.php.net to get a copy of this message
At 12:04 PM 3/21/2005, Marquez Design wrote:
Greetings, Does anyone know how to get a particular option to display in a drop menu? <select name="category" id="category"> <option selected="true" value="Option Value">Option Value</option> <option value="line">---------------------</option> <option value="value1 ">value1</option> <option value="value2 ">value2 </option> <option value="value3 ">value3</option> <option value="value4 ">value4</option> </select> The user has previously selected a category. That information is in the database. Here they are editing the record. What I would like is for the option that was selected and is in the database to be displayed as the selectd option. Does anyone know how I can do this, or can you point me in the right direction? Thank you, -- Steve Marquez Marquez Design
Steve, These can be a PITA. Here's how I've done it. 1. Create a function to determine which is selected: function is_selected( $option_expression ) {
        if ($option_expression)
        {
                $retval = "selected";
        }
        else
        {
                $retval = "";
        }
        return $retval;
}       // end of function is_selected
2. Build your list of selection, in this case a list of classifications fetched from a database; $option_class is the variable holding the list of options: $sql = "select * from class"; $result = mysql_query( $sql ); while( $row = mysql_fetch_array( $result ) )
        {
                 $nClassKey = $row["nClassKey"];
                 $cClass = $row["cClass"];
                 $selectval = is_selected( $nClassKey == $nClass );
                 $option_class .= "<option value = \"$nClassKey\" $selectval >$cClass</option>";
         }  // end while $row = mysql_fetch...
3. Then display it this way in the form: <Select name="nClass">
        <? echo $option_class ?>
</select> Hope this is helpful - Miles

« previous php.general (#211199) next »