7

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;
}

3 Answers 3

11

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
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You need to use '' as the key or 0 will be passed in the form value. i.e. $dropDownList[''] = 'Please Select'; Unless you want that..?
@Madmartigan, thanks, done, also updated my answer to include row number check.
You don't need that as result() always returns an array. It's pointless. Returning false maybe isn't a good idea for a function that should return an array, an empty array might be more appropriate. So +1 and -1 :/
7

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;
}

3 Comments

Better yet, define $dropDownList = array('' => 'Please Select'); first in case there's no DB results. Plus the tiny tiny boost of not calling a function.
+1 - also, just use validation to keep the empty result out of the database and force a selection (if needed).
@BigJobbies: No prob. This is how I do it on my site :-)
0

There is also the possibility to change the form_helper.php and to add something like:

 $form .= '<option value =0 >...</option>';

to the form_dropdown func. However, I do not get it selected as default but the second entry in the form.

But the problem is that you change the core files which should not be down generally.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.