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? Quote 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? Quote 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!"); Quote 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 Quote 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"? Quote 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>"; Quote 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! Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.