2 回答
TA贡献1827条经验 获得超8个赞
将复选框值存储在具有唯一ID的单独表中,将复选框中的id值设置为值,并将字段名称显示为标签
因为将来在某些情况下必须更改复选框名称,以便可以轻松更改该特定 ID 行数据。当您尝试使用逗号分隔符保存时,这是不可能的。
TA贡献1818条经验 获得超3个赞
在这里,我找到了一种方法来做到这一点。希望它能帮助别人。谢谢
控制器
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');
}
}
型
public function set_compactibility_chart_master(){
$post = $this->input->post();
for($z=0;$z<count($post['category']);$z++){
$sub_cat = "";
foreach($this->input->post('catcat'. $z) as $values){
$sub_cat .= $values.",";
}
$exp = explode(",",$sub_cat);
$newexp = array_unique($exp);
$newexp = implode($newexp, ',');
$insertdata = array(
'category' => $post['category'][$z],
'category_compact' => $newexp,
);
$this->db->where('category',$post['category'][$z]);
$exist = $this->db->get('tbl_compactibility_master');
$exist = $exist->num_rows();
if($exist == 0){
$this->db->insert('tbl_compactibility_master',$insertdata);
}else{
$this->db->where('category',$post['category'][$z]);
$this->db->update('tbl_compactibility_master',$insertdata);
}
}
return true;
}
视图
<form name="compatibility_form" id="compatibility_form" method="post" enctype="multipart/form-data" data-parsley-validate>
<table class="table table-bordered" width="100%">
<?php $cc=0; 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 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<?=$cc;?>[]" value="<?=$cat_res->id;?>" <?php if($cat_results->id == $cat_res->id){ ?> checked="checked" <?php } ?> <?php if(in_array($cat_res->id,$get_single)){ ?>checked="checked"<?php } ?> data-forcheck="<?=$cat_results->id;?>">
<label class="form-check-label"><?=$cat_res->category_name;?></label>
</div>
</div>
<?php } ?>
</div>
</td>
</tr>
<?php $cc++; } ?>
</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>
函数 get_single_record_compactibility()
public function get_single_record_compactibility($cat){
$this->db->where('category',$cat);
$result = $this->db->get('tbl_compactibility_master');
$result = $result->row();
if(!empty($result)){
return explode(",",$result->category_compact);
}else{
return array();
}
}
脚本
$('.checkedme').on("click",function() {
var getVal = $(this).val();
var test = $(this).data('forcheck');
$('#company_form').find("input[data-forcheck='"+getVal+"'][value='"+test+"']").prop('checked',this.checked);
});
- 2 回答
- 0 关注
- 74 浏览
添加回答
举报