我正在一个项目中,我需要使用Php CodeIgniter将多个选择的值添加到单个列中。 这是实际的问题陈述: 我有一个INT列 total_persons 来存储总人数(即5),还有一个VARCHAR列 person_names 可以存储这5个人的姓名。
要从下拉列表中选择多个用户,我正在使用Select2库。 但是当我提交数据时,弹出以下错误消息
“人名”字段为必填项。 当然是因为我在表单上设置了验证规则,如下所示:
$ this-> form_validation-> set_rules('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_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字段中添加五个名称,如下所示? [“ user1”,“ user2”,“ user3”,“ user4”,“ user5”] (这是我的假设,如果我猜错了,表示歉意)
有人可以帮我弄清楚我在哪里犯错吗?
非常感谢。
答案 0 :(得分:2)
CodeIgniter表单验证类支持use of arrays as field names。为了使用它,您需要在验证规则中包括[]
:
$this->form_validation->set_rules('person_names[]','Person Names','required');
还有表单错误:
form_error('person_names[]')
修改
为了将数组保存在单个列中,您可以先json_encode,以获取json格式的字符串,然后可以使用json_decode将数据作为数组检索:
$params = array(
'expense_type' => $this->input->post('expense_type'),
'total_persons' => $this->input->post('total_persons'),
'person_names' => json_encode($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'),
);
答案 1 :(得分:0)
通过这种方式,您可以通过用空格分隔多个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.