I'm working on web app using codeigniter framework and setting privileges for different type of users so I need to add approximately 35 rows in db for each user and each row have 6 checkbox values (0,1). I'm having difficulty while storing these values in array and sending to MODEL for insertion.
Here is Controller code:
public function set_priv(){
$data = array(
array(
'type_id' => $this->input->post('priv_type_id'),
'module_name' => 'Classes',
'can_access' => $this->input->post('classes[0]'),
'can_insert' => $this->input->post('classes[1]'),
'can_delete' => $this->input->post('classes[2]'),
'can_update' => $this->input->post('classes[3]'),
'can_export' => $this->input->post('classes[4]'),
'can_print' => $this->input->post('classes[5]'),
),
// ),
);
}
HTML Form Code for understanding Each check box for access, insert, update and delete privilege etc.
<div class="col-md-1">
<input type="checkbox" name="classes[]" />
</div>
<div class="col-md-1">
<input type="checkbox" name="classes[]" />
</div>
<div class="col-md-1">
<input type="checkbox" name="classes[]" />
</div>
<div class="col-md-1">
<input type="checkbox" name="classes[]" />
</div>
<div class="col-md-1">
<input type="checkbox" name="classes[]" />
</div>
<div class="col-md-1">
<input type="checkbox" name="classes[]"/>
</div>
Why you are using multidimensional array to insert the data? if you are getting checkbox value in array finely. I think it should be like.
$data = array(
'type_id' => $this->input->post('priv_type_id'),
'module_name' => 'Classes',
'can_access' => $this->input->post('classes[0]'),
'can_insert' => $this->input->post('classes[1]'),
'can_delete' => $this->input->post('classes[2]'),
'can_update' => $this->input->post('classes[3]'),
'can_export' => $this->input->post('classes[4]'),
'can_print' => $this->input->post('classes[5]'),
);
If you are adding checkboxes on view using any array then you can use same array to compare which checkbox is checked or which is not.
for example here is array to add checkboxes on view
$data[0] = 'can_access';
$data[1] = 'can_delete';
$data[2] = 'can_update';
on your view your code will be
<?php foreach($data as $key => $value){ ?>
<div class="col-md-1">
<input type="checkbox" name="classes[<?php echo $key; ?>]" />
</div>
<?php } ?>
after form submission you have to use same array to check i.e. $data array. code of your controller after submission will be
$builddata = [];
foreach($data as $key =>$value){
if(isset($this->input->post('classes['.$key.']'))){
$builddata[$value] = true;
}else{
$builddata[$value] = false;
}
}
printr($builddata);
your array will build like this
$builddata['can_access'] = true/false;
$builddata['can_delete'] = true/false;
$builddata['can_update'] = true/false;
You can't access post data like that. See below code
public function set_priv(){
$classes = $this->input->post('classes');
$data = array(
array(
'type_id' => $this->input->post('priv_type_id'),
'module_name' => 'Classes',
'can_access' => $classes[0],
'can_insert' => $classes[1],
'can_delete' => $classes[2],
'can_update' => $classes[3],
'can_export' => $classes[4],
'can_print' => $classes[5],
),
// ),
);
}