views:

59

answers:

1

I have a simple Crud application where there is one filed called Category. During Creation , for Category I have provided a simple drop down box which lists all categories. During Editing, I need to get the same drop down box with the entry in the database chosen.How do i do this.

For creation i used say

<p><b>Category:</b>&nbsp;&nbsp;&nbsp;<select name="category">
<option>Lifestyle</option>
<option>Automobiles</option>
<option>Food & Beverage</option>
<option>Health</option>
</select>

How do i do this during editing, say I need to select 'Health' which is available in the variable $row['course detail']

+2  A: 

The selected property of the option tag can be made use of:

<option selected='selected'>Health</option>

Code similar to the following should work:

<p><b>Category:</b>
<select name="category">
<?php
    $variable = 'Health';
    $options = array( 'Lifestyle', 'Automobiles', 'Food &amp; Beverage', 'Health' );
    foreach ( $options as $option ) {
        if ( $variable == $option ) {
            print "\t<option selected='selected'>$option</option>\n";
        }
        else {
            print "\t<option>$option</option>\n";
        }
    }
?>
</select>
Alan Haggai Alavi
How will i know if this is selected. It is dynamic...
Pal
Added sample code.
Alan Haggai Alavi
$selected = $variable == $option ' selected' : '';echo "<option{$selected}>{$option}</option>
fabrik