I have a categories of Chemicals, need to check the compatibility of each category of Chemicals with Self and Others. A loop inside a loop with Check boxes. 1. Unable to set value on click of the hidden element through javascript; 2. How do i proceed and optimize as the data for 24 Categories goes upto 14000+ odd entries. 3. A comma separated value or individual entry is suggested? 4. How to Edit the record and keep checkbox checked when fetching data from Database Table on the same Page
Here is the Code:
Controller
public function compactibility_chart_master(){
$data['title'] = 'Compactibility Chart Master';
$this->form_validation->set_rules('category[]', 'Category Name','required|trim|xss_clean');
if($this->form_validation->run() === FALSE){
$data['category'] = $this->admin_model->get_all_active_entities('tbl_category');
$this->load->view('admin/compactibility_chart_master',$data);
}else{
$this->admin_model->set_compactibility_chart_master();
$this->session->set_flashdata('success','Compacitibiliy Chart Master updated Successfully');
redirect('compactibility_chart_master', 'refresh');
}
}
Model
public function set_compactibility_chart_master(){
$post = $this->_array_remove_null($this->input->post());
$insertdata = array();
for($z=0;$z<count($post['cat_cat']);$z++){
foreach($post['cat_cat'] as $values){
$insertdata[] = array(
'category' => $post['category'][$z],
'category_compact' => $values,
);
}
}
$this->db->insert_batch('tbl_compactibility_master',$insertdata);
return true;
}
View
<form name="compatibility_form" id="compatibility_form" method="post" enctype="multipart/form-data" data-parsley-validate>
<table class="table table-bordered" width="100%">
<?php if(!empty($category)){ foreach($category as $cat_results){ ?>
<tr class="data">
<th>
<button class="btn btn-primary btn-sm my-2" type="button"><?=$cat_results->category_name;?></button>
<input type="hidden" name="category[]" class="categooryclass" value="<?=$cat_results->id;?>">
</th>
<td>
<div class="row">
<?php if(!empty($category)){ foreach($category as $key => $cat_res){ $get_single = $this->admin_model->get_single_record_compactibility($cat_results->id); ?>
<div class="col-md-6">
<div class="form-check-inline">
<label class="form-check-label">
<input type="checkbox" class="form-check-input my-2 checkedme" name="catcat[]" value="<?=$cat_res->id;?>" <?php if($cat_results->id == $cat_res->id){ ?> checked="checked" disabled="disabled"<?php } ?> data-forcheck="<?=$cat_results->id;?>">
<?=$cat_res->category_name;?></label>
<input type="hidden" name="cat_cat[]" data-urus="<?=$cat_results->id;?>" value="">
</div>
</div>
<?php }} ?>
</div>
</td>
</tr>
<?php }} ?>
</table>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<button class="btn btn-primary btn-sm my-4" id="submit_button" type="submit">Save Data</button>
</div>
</div>
</div>
</form>
Script
$('.checkedme').on("click",function() {
var getVal = $(this).val();
var test = $(this).data('forcheck');
$('#compatibility_form').find("input[data-forcheck='"+getVal+"'][value='"+test+"']").prop('checked',this.checked);
$('#compatibility_form').find("input[name='cat_cat[]'][data-urus='"+getVal+"']").val(test);});
Store the checkbox values in separate table with unique id set the id value in checkbox as value and show the name of the field as label
Because in future you have to change checkbox name in some cases so you can easily change that particular id row data. It is not possible when you tried to save with comma separator.
Thanks.. Will try and let know..