【问题标题】:Code Igniter - form_dropdown selecting correct value from the databaseCodeigniter - form_dropdown 从数据库中选择正确的值
【发布时间】:2010-12-02 02:17:15
【问题描述】:

我在 CodeIgniter 中的 form_dropdown 函数存在一些问题 .... 我的应用程序分为两部分,用户进入,输入表单并提交 .... 提交后,管理员可以进入并编辑该人形成,然后将其保存到数据库。

因此,要以初始形式显示下拉列表,我使用以下内容(下拉列表中的所有选项都来自数据库)

型号:

    function get_salaries_dropdown()
{
    $this->db->from($this->table_name);
    $this->db->order_by('id');
    $result = $this->db->get();
    $return = array();
    if($result->num_rows() > 0){
            $return[''] = 'please select';
        foreach($result->result_array() as $row){
            $return[$row['id']] = $row['salaryrange'];
        }
    }
    return $return;
}

然后在控制器中:

$data['salaries'] = $this->salary_expectation->get_salaries_dropdown();

最后是视图:

<?php echo form_dropdown('salaries', $salaries, set_value('salaries', $salaries));  ?>

该位非常适合显示填充了供用户选择的值的下拉列表。

所以,当用户选择一个值,然后点击保存,它就会保存到数据库中。

在管理员看到的编辑页面上,我使用相同的代码来显示充满选项的下拉列表,但我如何让它自动选择用户在初始阶段选择的那个?

干杯,

【问题讨论】:

  • 来自 codeigniter 文档 - The first parameter will contain the name of the field, the second parameter will contain an associative array of options, and the third parameter will contain the value you wish to be selected. 您在第三个参数中使用的 set_value 方法是什么?
  • 我找到了这个codeigniter解决方案github.com/thiswolf/codeigniter-enum-select-boxes

标签: codeigniter


【解决方案1】:

根据 Codeigniter 文档

第一个参数将包含 字段名,第二个 参数将包含一个关联 选项数组,第三个 参数将包含您的值 希望被选中。你也可以通过 多个项目的数组通过 第三个参数,CodeIgniter 将 为您创建多项选择。

你的管理控制器应该有类似的东西

$data['selected'] = $this->salary_expectation->get_salary_selected();

据此,管理视图应该是这样的

<?php echo form_dropdown('salaries', $salaries, $selected_value);  ?>

【讨论】:

    【解决方案2】:

    选择由 form_helper 的form_dropdown() 函数生成的&lt;select&gt;&lt;option&gt; 元素的一个讨厌的解决方案是使用发送的帖子输入。 我这样做是因为我发现的任何解决方案都没有显示用户在 set_selected 和 set_vaule 的形式中选择的值。 好吧,在我的控制器中,我有:

    $countries = $this->country_model->get_dropdown_array(); // The array have something like $countries[COUNTRY_ID] = COUNTRY_NAME
    $data['countries']=$countries;
    

    在我看来:

    $selected_country = $this->input->post('country');
    echo form_dropdown('country',$countries,$selected_country);
    

    而且工作正常!!! :)

    【讨论】:

      【解决方案3】:

      form_dropdown 函数具有用于选定选项的第三个参数。像这样使用它:

      <?php echo form_dropdown('piece_type', 
              array(
                  'type1' => 'Firts type',
                  'type2' => 'Second option'
                  $selected_value, 
                  'id = "piece_type"') ?>
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题,但我已经使用代码点火器语法克服了这个问题。 这是解决方案。 第一步 循环前初始化两个数组

        $options = array();
        $select = array();
        

        然后在循环中写这条指令

        foreach($result->result_array() as $row)
        {
            /////////Your Condition ////////////
            if($row['id'] == $myarray['mycolumn'])
            {            
                $options [$row['id']] = $row['salaryrange'];
                $select = $row['id'] ; 
            }else{
                $options [$row['id']] = $row['salaryrange'];
            }
        }
        

        现在

        echo form_dropdown('dropdown_name' , $options , $select);
        

        一切正常

        【讨论】:

          【解决方案5】:

          对于更新情况,您已经将相应的值传递给视图,如果传递变量类似于 $ind_post(from controller ),则编写如下代码:

          <?php echo form_dropdown('salaries', $salaries, $ind_post->salaries,'');  ?>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-06-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多