86Stang Posted August 1, 2008 Share Posted August 1, 2008 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? Link to comment https://forums.phpfreaks.com/topic/117744-solved-showing-the-selected-option-instead-of-its-value/ Share on other sites More sharing options...
lemmin Posted August 1, 2008 Share Posted August 1, 2008 Could you show your sorting code? Link to comment https://forums.phpfreaks.com/topic/117744-solved-showing-the-selected-option-instead-of-its-value/#findComment-605607 Share on other sites More sharing options...
86Stang Posted August 1, 2008 Author Share Posted August 1, 2008 Sure thing: $qry = "SELECT * FROM table ORDER BY $sort_option"; $result = mysql_query($qry) or die("Error during query!"); Link to comment https://forums.phpfreaks.com/topic/117744-solved-showing-the-selected-option-instead-of-its-value/#findComment-605612 Share on other sites More sharing options...
Barand Posted August 1, 2008 Share Posted August 1, 2008 Instead of adding start_date twice, loop through the options and set the one whose value is "start_date" as SELECTED Link to comment https://forums.phpfreaks.com/topic/117744-solved-showing-the-selected-option-instead-of-its-value/#findComment-605628 Share on other sites More sharing options...
86Stang Posted August 1, 2008 Author Share Posted August 1, 2008 But is that going to fix my original problem of wanting to show "Start Date" instead of "start_date"? Link to comment https://forums.phpfreaks.com/topic/117744-solved-showing-the-selected-option-instead-of-its-value/#findComment-605668 Share on other sites More sharing options...
.josh Posted August 1, 2008 Share Posted August 1, 2008 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>"; Link to comment https://forums.phpfreaks.com/topic/117744-solved-showing-the-selected-option-instead-of-its-value/#findComment-605708 Share on other sites More sharing options...
86Stang Posted August 1, 2008 Author Share Posted August 1, 2008 Oooooh!! I totally misunderstood Barand! That solved it, thanks! Link to comment https://forums.phpfreaks.com/topic/117744-solved-showing-the-selected-option-instead-of-its-value/#findComment-605757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.