Reputation: 429
I have a question concerning select forms. I have a variable containing a string extracted from a database, and i want the select form to priority this first, and make it the top of the select form.
$smode = "fade";
<select name='smode'>
<?php
$modes = array('horizontal', 'vertical', 'fade');
$i = 0;
while($i < 3){
echo "<option value='$modes[$i]'>$modes[$i]</option>";
$i++;
}
?>
</select>
So when the variable $smode is "fade", i want the fade option-field at the top of the select form, when the variable $smode is "vertical" i want the vertical option-field at the top, and so on.
Hope someone can help me. Thanks.
Upvotes: 1
Views: 154
Reputation: 6202
This question comes down to array manipulation.
$smode = "fade";
$modes = array('horizontal', 'vertical', 'fade');
//remove the smode term from the array
$modes = array_diff($modes, array($smode) );
// ... and add it to the beginning
array_unshift($modes, $smode);
Working example: https://eval.in/123063
Upvotes: 0
Reputation: 2511
You can simply do the following:
$modes = array('horizontal', 'vertical', 'fade');
$modes = array_diff( $modes, array($smode) );// create a new array without the top option
array_unshift($modes, $smode);//add the option to first position
//carry on...
Upvotes: 0
Reputation: 20469
Simple remove the primary item from the array, add it 1st, than add the rest
$smode = "fade";
echo "<select name='smode'>";
$modes = array('horizontal', 'vertical', 'fade');
//remove the primary item
$fixedmodes=array_diff($modes, array($smode));
//add the primary select option
echo "<option value='$smode'>$smode</option>";
//add the rest
foreach ($fixedmodes as $val) {
echo "<option value='$val'>$val</option>";
}
echo "</select>";
Upvotes: 0
Reputation: 37408
This should do the trick. Just store your output in variables. Define one for your top and one for the rest. After your loop, you just output them in your desired order using echo.
$smode = "fade";
<select name='smode'>
<?php
$modes = array('horizontal', 'vertical', 'fade');
$i = 0;
$options = '';
$topOption= '';
while($i < 3){
if($modes[$i] == $smode) {
$topOption = "<option value='$modes[$i]' selected='selected'>$modes[$i]</option>"
} else {
$options .= "<option value='$modes[$i]'>$modes[$i]</option>";
}
$i++;
}
echo $topOption . $options;
?>
</select>
Also, read up on the for loop.
$smode = "fade";
<select name='smode'>
<?php
$modes = array('horizontal', 'vertical', 'fade');
$options = '';
$topOption= '';
for($i =0; $i < 3;$i++){
if($modes[$i] == $smode) {
$topOption = "<option value='$modes[$i]' selected='selected'>$modes[$i]</option>"
} else {
$options .= "<option value='$modes[$i]'>$modes[$i]</option>";
}
}
echo $topOption . $options;
?>
</select>
Or even consider foreach:
$smode = "fade";
<select name='smode'>
<?php
$modes = array('horizontal', 'vertical', 'fade');
$options = '';
$topOption= '';
foreach($modes as $modeItem){
if($modeItem == $smode) {
$topOption = "<option value='$modeItem' selected='selected'>$modeItem</option>"
} else {
$options .= "<option value='$modeItem'>$modeItem</option>";
}
}
echo $topOption . $options;
?>
</select>
Upvotes: 2
Reputation: 1373
This would also be a solution (haven't tested, should work)
$smode = "vertical";
function cmp($a, $b) {
global $smode;
if ($b == $smode) {
return 1;
}
return 0;
}
$modes = array('horizontal', 'vertical', 'fade');
usort($modes, 'cmp');
print_r($modes);
Upvotes: 0