简体   繁体   English

如何使用Codeigniter用数据库中的项目填充下拉列表?

[英]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. 即:0、1、2等。

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. 例如,当我选择名为“ test”的第一个选项时,它输出0(它在数组中的位置)而不是像我想要的那样输出“ test”。

How do I adjust what the value is when populating the dropdown? 填充下拉菜单时如何调整值?

thank you in advance. 先感谢您。

I am using Codeigniter 3. 我正在使用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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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