Populating Select Controls

Pass an array to the view from the controller. Loop through each item adding the item to the select box. If we submit the form we probably want to remember the values selected by the user

 

 <!-- Render Customer Names List Box -->

Client

All Customers customerID==$search[‘customer_id’]){ $selectedTag = “selected “; } else { $selectedTag = “”; } ?> value=”customerID ?>”>customerName ?>

</div>

Alternatively we can render the control based on a local array

 <!-- Report Option -->
$report = array(
     array('value' => '1', 'label' => 'Billed'),
     array('value' => '2', 'label' => 'Paid'),
     array('value' => '3', 'label' => 'Outstanding')
);

foreach($report as $item) {
     if($item['value']==$search['report_id']){
          $selectedTag = "selected";
     } else {
          $selectedTag = "";
     }

     echo "< option " . $selected . $item['label'] . "";
}

We pass the value of the report ID into the view i the variable $search[‘report_id’]

Leave a comment