简体   繁体   中英

How to fill populate a dropdown form with items from the database using codeigniter?

I am attempting to create a dropdown filled with items from my database.

I have it populating but the values are ending up being the position in the array. ie: 0, 1, 2, etc.

Here is my code for the form:

 echo form_open('main/generate_report');

    $depts = array();

    foreach ($my_depts->result() as $row)
    {

        $depts[] = $row->DEPT_NAME;

    }

    echo form_dropdown('dept_select', $depts);

    echo form_submit('report_submit', 'Generate Report');

it goes to this function when submitted:

echo '<h1><u>Report</u></h1>';
            echo '<h2>';
            echo $this->input->post('dept_select');
            echo '</h2>';

for example when I select the first option named "test", it outputs 0 (its position in the array) rather than "test" like I want it to.

How do I adjust what the value is when populating the dropdown?

thank you in advance.

I am using Codeigniter 3.

Try below code. Use associative array for dropdown.

echo form_open('main/generate_report');

$depts = array();

foreach ($my_depts->result() as $row)
{

    $depts[$row->DEPT_NAME] = $row->DEPT_NAME;

}

echo form_dropdown('dept_select', $depts);

echo form_submit('report_submit', 'Generate Report');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM