Jump to content

[SOLVED] Showing the selected option instead of its value


86Stang

Recommended Posts

I have a drop down that lets the user sort the records.  It looks like this:

 

<select name=\"sort_option\">
<option value="<?php echo $sort_option; ?>" SELECTED><?php echo $sort_option;?></option> 
<option value=\"business\">Business Name</option>
<option value=\"category\">Category</option>
<option value=\"start_date\">Start Date</option>
</select>

 

My problem is when it sorts, the selected option shows as the value instead of the name. In other words when the user sorts by start_date, on the following page it shows "start_date" as the first option of the drop down and I'd like it to show "Start Date".  Might this be an easy fix?

As Barand said:

 


// example array to loop options
$options = array('business' => 'Business Name',
                       'category' => 'Category',
                       'start_date' => 'Start Date');


echo "<select name='sort_option'>";

foreach ($options as $name => $value) {
   $selected = ($name == $sort_option)? "SELECTED" : "";
   echo "<option value='$name' $selected>$value</option>";
}

echo "</select>";

Archived

This topic is now archived and is closed to further replies.

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