I'm wondering if someone can help me out. I have a form_dropdown which I'm filling with options from the database. I want to add a default "Please Select" value at the top of the list, but can't figure out how to do it without adding that to the database.
My code is as follows:
function populatedropdown()
{
$query = $this->db->query('SELECT * FROM '.$this->table_name.' WHERE active=1 ORDER BY brand');
$dropdowns = $query->result();
foreach($dropdowns as $dropdown) {
$dropDownList[$dropdown->brand] = $dropdown->brand;
}
$finalDropDown = $dropDownList;
return $finalDropDown;
}
Another way to write it would be:
function populatedropdown()
{
$query = $this->db->query('SELECT * FROM '.$this->table_name.' WHERE active=1 ORDER BY brand');
if ($query->num_rows() > 0) {
$dropdowns = $query->result();
$dropDownList[''] = 'Please Select'; // default selection item
foreach($dropdowns as $dropdown) {
$dropDownList[$dropdown->brand] = $dropdown->brand;
}
return $dropDownList;
} else {
return false; // return false if no items for dropdown
}
}
Just add it to the array before you return it.
function populatedropdown()
{
$query = $this->db->query('SELECT * FROM '.$this->table_name.' WHERE active=1 ORDER BY brand');
$dropdowns = $query->result();
foreach($dropdowns as $dropdown) {
$dropDownList[$dropdown->brand] = $dropdown->brand;
}
$finalDropDown = array_merge(array('' => 'Please Select'), $dropDownList);
return $finalDropDown;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With