【问题标题】:How to fill populate a dropdown form with items from the database using codeigniter?如何使用 codeigniter 使用数据库中的项目填充下拉表单?
【发布时间】:2016-05-21 16:24:12
【问题描述】:

我正在尝试创建一个下拉列表,其中包含我的数据库中的项目。

我有它填充,但值最终成为数组中的位置。即:0、1、2等。

这是我的表单代码:

 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');

提交的时候转到这个函数:

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

例如,当我选择名为“test”的第一个选项时,它会输出 0(它在数组中的位置),而不是像我想要的那样输出“test”。

填充下拉列表时如何调整值?

提前谢谢你。

我正在使用 Codeigniter 3。

【问题讨论】:

    标签: php forms codeigniter


    【解决方案1】:

    试试下面的代码。使用关联数组进行下拉。

    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');
    

    【讨论】:

    • 美丽。工作完美。非常感谢
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多