dongrong7883 2017-02-07 06:00
浏览 33
已采纳

如何在codeigniter中的数据库中存储多个选择框值?

I have one requirements form in my page..in that form i have users dropdown with select box. I want to select multiple users at a time and need to store selected users in database.

Controller:

public function requirement()
{
    $this->load->model('LoginModel');
    $data['user'] = $this->LoginModel->getusers();
    $this->load->view('Admin/requirements',$data);

    $insert = array (
      'role_name'             => $this->input->post('role_name'),
      'vacancies'             => $this->input->post('vacancies'),
      'experience'            => $this->input->post('experience'),
      'jd'                    => $this->input->post('jd'),
      'hiring_contact_name'   => $this->input->post('hiring_contact_name'),
      'hiring_contact_number' => $this->input->post('hiring_contact_number'),
      'user_id'               => $this->input->post('user_id')//this is my foreign key id from users table);
    );
    $this->LoginModel->add_requirement($insert);
}

Model:

function getusers()
{
    $this->db->select('*');
    $this->db->from('users');
    $query = $this->db->get();
    //echo $this->db->last_query();
    return $query->result();
}

View page

<div class="form-group">
    <label>Choose Vendor</label>
    <select id="chkveg"class="form-control" multiple class="form-control" data-placeholder="user name"  name="user_id[]" >
        <option value="0"></option>
        <?php
        print_r($user);
        foreach ($user as $rows)
        {
            ?>
            <option value="<?php echo $rows->user_id ?>">
                <?php echo ucfirst($rows->first_name) ?>
            </option>
            <?php
        }
        ?>
    </select>
</div>
  • 写回答

2条回答 默认 最新

  • dongzhuo3059 2017-02-07 06:13
    关注

    make select-box array instead of single value.

    <select id="chkveg""  class="form-control" multiple class="form-control" data-placeholder="user name"  name="user_id[]" >
    

    than you need to ALTER your column datatype to varchar or text.

    after that you can implode function to create string value like

    'user_id'=> implode(',',$this->input->post('user_id')); //eg.1,2,3
    

    Reference link : http://www.w3schools.com/php/func_string_implode.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent模拟静态气体扩散
  • ¥15 java根据模板,生成word文档,需要带目录
  • ¥15 广告联盟的兜底广告是什么意思
  • ¥15 如何证明高斯噪声的包络公式
  • ¥150 寻找王者荣耀开发作者,合作或者解答
  • ¥15 乳腺癌数据集 相关矩阵 特征选择
  • ¥15 我的游戏账号被盗取,请问我该怎么做
  • ¥15 通关usb3.0.push文件,导致usb频繁断连
  • ¥15 有没有能解决微信公众号,只能实时拍照,没有选择相册上传功能,我不懂任何技术,,有没有给我发个软件就能搞定的方法
  • ¥15 Pythontxt文本可视化