Reputation: 545
I'm working on a project where I need to add multiple selected values into a single Column using Php CodeIgniter. Here's the actual problem statement: I have a INT column total_persons to store the number of total people (i.e. 5) and a VARCHAR column person_names which would store the names of those 5 people.
To select multiple users from dropdown, I'm using Select2 library. But when I submit the data, following error pops up
The Person Names field is required. Of course it is because I've set validation rules on the form as follows:
$this->form_validation->set_rules('person_names','Person Names','required');
I don't understand why it does not take any value when there are 5 user names selected.
If I add ID of a single user (keeping the column to be INT) instead of multiple selected values, it works fine and sends data in DB. But when I try to store multiple usernames, it throws the error I pasted above.
Here's my Expense.php code from Controller
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>
If I'm not missing some key point, the code should add five names in the user_name field as follows? ["user1", "user2", "user3", "user4", "user5"] (This is my assumption, apologies if I've guessed it wrong)
Can someone help me figure out where am I making a mistake?
Thanks very much in advance.
Upvotes: 0
Views: 1465
Reputation: 130
In this way you can save your multiple persons ids in single column by seperating them with space .
You can get the saved ids by taking ids into array and using jquery you can explode it and show in multiple select field.
<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.
Upvotes: 0
Reputation: 4719
CodeIgniter Form Validation class supports the use of arrays as field names. In order to use it, you need to include the []
on the validation rules :
$this->form_validation->set_rules('person_names[]','Person Names','required');
And also on the form error :
form_error('person_names[]')
Edit
In order to save the array in a single column, you could json_encode it first, to get json formatted string which you could retrieve the data later as array using 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'),
);
Upvotes: 2