Php option value

2013 年 11 月 17 日3790

Use an array:

$areas = array(



'apple' => 'Apple',



'lemon' => 'Lemon',



'orange' => 'Orange',



'banana' => 'Banana'



);



Then use that array to print the select:

<select>



<?php foreach ($areas as $value => $text): ?>



<option value="<?php echo $value; ?>" <?php if ($user_data[$area] == $value) {echo 'selected';} ?>><?php echo $text; ?>



</option>



<?php endforeach; ?>



</select>



I am using an associative array because I am assuming that you want to be able to customize the areas' text label, and that they will not only be a capitaized version of the value used to match the user data.

0 0