Mysql
 sql >> Database >  >> RDS >> Mysql

Crea un gruppo opt da un array di dati

Prova, prima riformatta l'array di origine come di seguito:

$result = array();
foreach($depots as $depot){
   $result[$depot->operating_company_name][] = $depot;
}

Quindi per creare la prova selezionata,

<select name="depot_id" class="form-control">
            <?php foreach($result as $key=>$val): ?>
                    <optgroup label="<?php echo $key; ?>">
                       <?php foreach($val as $option): ?>
                        <option value="<?php echo $option->depot_id; ?>"><?php echo $option->depot_name; ?></option>
                         <?php endforeach; ?>
                    </optgroup>
            <?php endforeach; ?>
        </select>