如何在PHP CodeIgniter中将多个选定值添加到单个列中?

如何在PHP CodeIgniter中将多个选定值添加到单个列中?,php,codeigniter,jquery-select2,Php,Codeigniter,Jquery Select2,我正在进行一个项目,需要使用Php CodeIgniter将多个选定值添加到一个列中。 以下是实际问题陈述: 我有一个INT列total_persons来存储总人数(即5人),还有一个VARCHAR列persons_names来存储这5人的姓名 要从下拉列表中选择多个用户,我使用Select2库。 但当我提交数据时,会弹出以下错误 人名字段是必需的。 当然,这是因为我在表单上设置了如下验证规则: $this->form_validation->set_规则('person_names'、'per

我正在进行一个项目,需要使用Php CodeIgniter将多个选定值添加到一个列中。 以下是实际问题陈述: 我有一个INT列total_persons来存储总人数(即5人),还有一个VARCHAR列persons_names来存储这5人的姓名

要从下拉列表中选择多个用户,我使用Select2库。 但当我提交数据时,会弹出以下错误

人名字段是必需的。 当然,这是因为我在表单上设置了如下验证规则:

$this->form_validation->set_规则('person_names'、'person names'、'required')

我不明白为什么当选择了5个用户名时它不带任何值

如果我添加单个用户的ID(将列保持为INT)而不是多个选定值,那么它可以正常工作并以DB形式发送数据。但是当我尝试存储多个用户名时,它抛出了我在上面粘贴的错误

这是我从Controller获得的Expense.php代码

    function add()
    {   
        $this->load->library('form_validation');

        $this->form_validation->set_rules('expense_type','Expense Type','required');
        $this->form_validation->set_rules('person_names','Person Names','required');
        $this->form_validation->set_rules('total_persons','Total Persons','required');
        $this->form_validation->set_rules('expense_amount','Expense Amount','required');
        $this->form_validation->set_rules('expense_details','Expense Details','required');
        $this->form_validation->set_rules('date_added','Date Added','required');

        if($this->form_validation->run())     
        {   
            $params = array(
                'expense_type' => $this->input->post('expense_type'),
                'total_persons' => $this->input->post('total_persons'),
                'person_names' => $this->input->post('person_names'),
                'expense_amount' => $this->input->post('expense_amount'),
                'expense_details' => $this->input->post('expense_details'),
                'date_added' => $this->input->post('date_added'),
                'update_date' => $this->input->post('update_date'),
            );

            print_r($params);
            exit();

            $expense_id = $this->Expense_model->add_expense($params);
            redirect('expense/index');
        }
        else
        {
            $this->load->model('Expense_type_model');
            $data['all_expense_type'] = $this->Expense_type_model->get_all_expense_type();

            $this->load->model('User_model');
            $data['all_users'] = $this->User_model->get_all_users();

            $data['_view'] = 'expense/add';
            $this->load->view('layouts/main',$data);
        }
    }  
Expense\u Model.php

    function add_expense($params)
    {
        $this->db->insert('expense',$params);
        return $this->db->insert_id();
    }
view.php

    <div class="col-md-6">
        <label for="person_names" class="control-label"><span class="text-danger">*</span>Person Names</label>
        <div class="form-group">
            <select name="person_names[]" class="form-control multiselect" multiple="multiple">
                <option value="">select user</option>
                    <?php 
                        foreach($all_users as $user)
                        {
                            $selected = ($user['user_name'] == $this->input->post('person_names')) ? ' selected="selected"' : "";

                            echo '<option value="'.$user['user_name'].'" '.$selected.'>'.$user['user_name'].'</option>';

                        } 
                    ?>
           </select>

            <span class="text-danger"><?php echo form_error('person_names');?></span>
        </div>
    </div>

*人名
选择用户
如果我没有遗漏一些关键点,代码应该在user_name字段中添加五个名称,如下所示? [“用户1”、“用户2”、“用户3”、“用户4”、“用户5”] (这是我的假设,如果我猜错了,很抱歉)

有人能帮我找出哪里出了错吗


非常感谢。CodeIgniter表单验证类支持。为了使用它,您需要在验证规则中包含
[]

$this->form_validation->set_rules('person_names[]','Person Names','required');
还有关于表单错误:

form_error('person_names[]')
编辑

为了将数组保存在一个列中,您可以首先使用它来获取json格式的字符串,然后使用以下命令将数据检索为数组:


CodeIgniter表单验证类支持。为了使用它,您需要在验证规则中包含
[]

$this->form_validation->set_rules('person_names[]','Person Names','required');
还有关于表单错误:

form_error('person_names[]')
编辑

为了将数组保存在一个列中,您可以首先使用它来获取json格式的字符串,然后使用以下命令将数据检索为数组:


通过这种方式,您可以在一列中保存多个人ID,方法是用空格分隔多个人ID

您可以通过将ID放入数组并使用jquery将其分解并显示在多个选择字段中来获取保存的ID

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select class="form-control" id="alertrecepients" name="alertrecepients" multiple size="10">
 <?php
    if(!empty($users)){
        foreach ($users as $cl){
 ?>
   <option value="<?php echo $cl->userId ?>"
<?php 
   $x = 0;
   $paramArray31=explode(' ',$alertrecepients);
   $limit= count($paramArray31);
   while($x <= $limit-1) {
      if ($paramArray31[$x] == $cl->userId) {
        echo "selected";
      }
      $x++;
   }
?>>
<?php echo $cl->name ?></option>
<?php }} ?>
</select>

<script>
    $( "select[name='alertrecepients']" )
    .change(function() {
    var str = "";
    $( "select[name='alertrecepients'] option:selected" ).each(function() {
    str += $( this ).val() + " ";
    });
    $( "#selectedrecepients" ).val( str );
    })
    .trigger( "change" );
</script>

<input type="hidden" value="<?php echo $alertrecepients; ?>" name="selectedrecepients" id="selectedrecepients" />

I am not familiar with snippet, so i am posting answer like this.


通过这种方式,您可以在一列中保存多个人ID,方法是用空格分隔多个人ID

您可以通过将ID放入数组并使用jquery将其分解并显示在多个选择字段中来获取保存的ID

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select class="form-control" id="alertrecepients" name="alertrecepients" multiple size="10">
 <?php
    if(!empty($users)){
        foreach ($users as $cl){
 ?>
   <option value="<?php echo $cl->userId ?>"
<?php 
   $x = 0;
   $paramArray31=explode(' ',$alertrecepients);
   $limit= count($paramArray31);
   while($x <= $limit-1) {
      if ($paramArray31[$x] == $cl->userId) {
        echo "selected";
      }
      $x++;
   }
?>>
<?php echo $cl->name ?></option>
<?php }} ?>
</select>

<script>
    $( "select[name='alertrecepients']" )
    .change(function() {
    var str = "";
    $( "select[name='alertrecepients'] option:selected" ).each(function() {
    str += $( this ).val() + " ";
    });
    $( "#selectedrecepients" ).val( str );
    })
    .trigger( "change" );
</script>

<input type="hidden" value="<?php echo $alertrecepients; ?>" name="selectedrecepients" id="selectedrecepients" />

I am not familiar with snippet, so i am posting answer like this.


这对我有用。谢谢这是view.php文件My bad的一个输入错误。现在它没有在编辑表单中设置所选名称。当我转到“编辑”页面时,所有字段都显示了选定值(从数据库中获取),但“人名”字段为空。为了编辑表单,我必须重新选择用户名。这对我很有效。谢谢这是view.php文件My bad的一个输入错误。现在它没有在编辑表单中设置所选名称。当我转到“编辑”页面时,所有字段都显示了选定值(从数据库中获取),但“人名”字段为空。我必须重新选择用户名才能编辑表单。