I am building my new website and have incorporated a select menu so the user can sort the results that get displayed by the PHP, it works well, however whenever the page first gets displayed the default option is always the last one in the code, so for the code below the option that gets displayed is the 'Free' option where it should be the 'None' option. Also using the same example as above if I went to select the name option the results get displayed correctly, however the option now in the box is still the free option, is there anyway I can change this to the option being used.
Sorry if this is a little confusing as I have two similar questions I need to get sorted.
Here is the HTML+PHP code.
<form method="get" style="display: inline;" name='orderby_form'>
<input type=hidden name='param1' value="<?php print $param1; ?>">
<input type=hidden name='param2' value="<?php print $param2; ?>">
<select name=orderby onChange="orderby_form.submit();">
<option value='name' <?php print $selected[$orderby]; ?>>Name</option>
<option value='difficulty' <?php print $selected[$orderby]; ?>>Difficulty</option>
<option value='payout' <?php print $selected[$orderby]; ?>>Payout</option>
<option value='newest' <?php print $selected[$orderby]; ?>>Newest</option>
<option value='free' <?php print $selected[$orderby]; ?>>Free</option>
<option> None</option>
</select>
</form>
Here is the PHP code:
$selected = array();
$orderby = $_GET[orderby];
if(!$orderby) { $orderby = 'name'; }
if($orderby == 'name')
{
$orderby_query = "ORDER BY name";
}
else if($orderby == 'difficulty')
{
$orderby_query = "ORDER BY difficulty_rating ASC";
}
else if($orderby == 'payout')
{
$orderby_query = "ORDER BY penny_value DESC";
}
else if($orderby == 'newest')
{
$orderby_query = "ORDER BY date_added DESC";
}
else if($orderby == 'free')
{
$orderby_query = "AND free='1'";
}
else { unset($orderby); }
// If $orderby was valid set the selected sort option for the form.
if($orderby)
{
$selected[$orderby] = 'selected';
}
The selected attribute in the HTML needs to appear as ....
Try changing this:
if($orderby){
$selected[$orderby] = 'selected';
}
TO:
if($orderby){
$selected[$orderby] = 'selected="selected"';
}
I hope this helps!
Change:
<option value='name' <?php print $selected[$orderby]; ?>>Name</option>
<option value='difficulty' <?php print $selected[$orderby]; ?>>Difficulty</option>
<option value='payout' <?php print $selected[$orderby]; ?>>Payout</option>
<option value='newest' <?php print $selected[$orderby]; ?>>Newest</option>
<option value='free' <?php print $selected[$orderby]; ?>>Free</option>
To:
<option value='name' <?php print $selected['name']; ?>>Name</option>
<option value='difficulty' <?php print $selected['difficulty']; ?>>Difficulty</option>
<option value='payout' <?php print $selected['payout']; ?>>Payout</option>
<option value='newest' <?php print $selected['newest']; ?>>Newest</option>
<option value='free' <?php print $selected['free']; ?>>Free</option>
Currently you are using the same value for 'selected' for each option. $selected[$orderby];
will have been selected if it was valid, and you'll be outputting 'selected' for every option . I suppose the browser could just decide to pick the last one to handle that.