What are some efficient technical approaches to displaying assigned customers as marked in a selectbox in PHP?
When displaying assigned customers as marked in a selectbox in PHP, one efficient approach is to retrieve the assigned customers from the database and then loop through all customers to generate the selectbox options. For each customer, check if they are assigned and mark them as selected in the selectbox.
<?php
// Assume $assignedCustomers is an array of assigned customer IDs
// Retrieve all customers from the database
$customers = getAllCustomers();
echo '<select multiple name="assigned_customers[]">';
foreach ($customers as $customer) {
$selected = (in_array($customer['id'], $assignedCustomers)) ? 'selected' : '';
echo '<option value="' . $customer['id'] . '" ' . $selected . '>' . $customer['name'] . '</option>';
}
echo '</select>';
?>