tags:

views:

46

answers:

5

I have a select that looks like this, it is written in in HTML and is not rendered via any php,

    <select name="position">                                           
<option value="left">Left</option>
    <option value="right">Right</option>
    <option value="centre">Centre</option>
    </select>

The value gets sent to database and later on gets returned from the database as a variable in the form of $v['position'], using this and my original form how can I make the option that matches the varibale the default selection?

+1  A: 

You didn't specify when the form displayed again. If immediately when user is submitted the form, you need to insert this snippet to every option:

<option value="left"<?php echo $v['position'] == 'left' ? ' selected' : ''; ?>>Left</option>
<option value="right"<?php echo $v['position'] == 'right' ? ' selected' : ''; ?>>Right</option>
<option value="centre"<?php echo $v['position'] == 'centre' ? ' selected' : ''; ?>>Centre</option>

OR:

You must iterate through variables via PHP :(

$html = '<select name="position">';

$opts = array('left', 'right', 'centre');
foreach($opts as $option)
{
    $html .= '<option value="' . $option . '"';
    $html .= $option == $v['position'] . ' selected' : '';
    $html .= '>' . ucfirst($option) . '</option>';
}
$html .= '</select>';

print $html;
fabrik
A: 
<select name="position">                                            
    <option value="left"<?php echo ($v['position'] == 'left') ? ' selected="selected" : ''; ?>>Left</option> 
    <option value="right"<?php echo ($v['position'] == 'right') ? ' selected="selected" : ''; ?>>Right</option> 
    <option value="centre"<?php echo ($v['position'] == 'centre') ? ' selected="selected" : ''; ?>>Centre</option> 
</select> 
Mark Baker
A: 

try this

<select name="position">                                           
    <option value="left" <?php echo $v['position']=='left'?'selected="selected"':'' ?> >Left</option>
    <option value="right" <?php echo $v['position']=='right'?'selected="selected"':'' ?>>Right</option>
    <option value="centre" <?php echo $v['position']=='centre'?'selected="selected"'?:'' >>Centre</option>
</select>
dejavu
+1  A: 

You can create the options in a loop and check whether the current element equals the value in $v['position'] and set the selected attribute accordingly.

<?php
$options = array('left'=>'Left', 'right'=>'Right', 'centre'=>'Centre');
?>

<select name="position">      
<?php foreach($options as $value=>$text):?>                                     
    <option value="<?php echo $value ?>" 
            <?php echo ($v['position'] == $value) ? 'selected="selected"' : '' ?> >
            <?php echo $text ?>
    </option>
<?php endforeach; ?>
</select>
Felix Kling
A: 

You can do it with DOM without having to touch your HTML. If this is your HTML:

$template = <<< TPL
<select name="position">
    <option value="left">Left</option>
    <option value="right">Right</option>
    <option value="centre">Centre</option>
</select>
TPL;

And this is the value that was selected:

$value = 'right';

You can do

$dom = new DOMDocument;
$dom->loadXml($template);
$xPath = new DOMXPath($dom);
$node = $xPath->query(sprintf('//option[@value = "%s"]', $value));
if($node->item(0)) {
    $node->item(0)->setAttribute('selected', 'selected');
}
echo $dom->saveXML($dom->documentElement);

And that will output:

<select name="position">
    <option value="left">Left</option>
    <option value="right" selected="selected">Right</option>
    <option value="centre">Centre</option>
</select>
Gordon