【问题标题】:Codeigniter insert multiple selected values into databaseCodeigniter 将多个选定的值插入数据库
【发布时间】:2013-11-21 16:49:51
【问题描述】:

我在这个多重选择数组中花费了很多时间,该数组在多重下拉框中保存多个值,而我想要将选定的值插入表中。

假设我从下拉框中选择了1,2,3,当我 print_r($this->input->post('category'))` 时,它会显示出来

Array ( [0] => 1 [1] => 2 [2] => 2 )

然而,当插入表时,它只插入最后一个值,而不是所有 3 个值。

这里是查看来选择几个值:

$category = array(
    'name' => 'category',
    'id' => 'category'
);

<select name="category[]" id="<?php echo $category['id'] ?>" multiple="multiple">
                    <?php
                    foreach($catOpts as $catOpt)
                    {
                        $selected = ($this->input->post('category')==$catOpt->category_name) ? 'selected' : '';
                        echo '<option value="'.$catOpt->category_id.'" '.$selected.'>'.$catOpt->category_name.'</option>';
                    }
                    ?>
                </select>

Controller 中,我将值传递给验证,如果验证有效,则:

$this->form_validation->set_rules('category[]', 'Category', 'required');

if($this->form_validation->run()) { // validation ok

    if(!is_null($data = $this->db_model->save_listing(          
        $this->form_validation->set_value('category[]')
    ))) { // success

    //some message to acknowledge success created.

    }
}

模型插入表格:

function save_listing($category)
{

    $data = array(
        'category_id' => $category
    );

    $this->db->insert('listing', $data);

    return TRUE;

}

我不知道如何将所有值(数组)传递给控制器​​$this-&gt;form_validation-&gt;set_value('category[]'),然后执行模型函数save_listing()并将所有值保存到数据库中的列中。

请帮助解决我的问题,我浏览了很多论坛,但没有找到解决方案。

谢谢。

【问题讨论】:

    标签: codeigniter insert


    【解决方案1】:

    当你的字段是一个数组时,你必须:

    $data= array();
    
    while( $v = $this->form_validation->set_value("field[]") )
    {
        $data[] =  $v;
    }
    

    如果你不这样做,它会返回最后一个值。

    您也可以通过$this-&gt;input-&gt;post('fields') 获取值,但您的理智规则不会应用于这些值,例如 htmlspecialchars。

    当然,这并没有像其他东西一样在文档中指定..

    来源/system/libraries/Form_validation.php:

    /**
     * Get the value from a form
     *
     * Permits you to repopulate a form field with the value it was submitted
     * with, or, if that value doesn't exist, with the default
     *
     * @access  public
     * @param   string  the field name
     * @param   string
     * @return  void
     */
    public function set_value($field = '', $default = '')
    {
        if ( ! isset($this->_field_data[$field]))
        {
            return $default;
        }
    
        // If the data is an array output them one at a time.
        //     E.g: form_input('name[]', set_value('name[]');
        if (is_array($this->_field_data[$field]['postdata']))
        {
            return array_shift($this->_field_data[$field]['postdata']);
        }
    
        return $this->_field_data[$field]['postdata'];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-27
      • 2015-07-02
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多