Tutorials

Creating HTML select easily in PHP

Creating a &;t;select> in PHP can be a mess, if done naively. This should make life a little easier.

$str="<select name='fries_with_that'>
<option value='true'>Yes</option>
<option value='false'>No</option>
</select>";

foreach($customer as $person_id=>$details) {
  $fries_with_that=$details['fries_with_that'];
  $str=str_replace("value='$fries_with_that'","value='$fries_with_that' selected",$str);
  echo str_replace("name='fries_with_that'","name='fries_with_that_$person_id'",$str);
}