2 回答

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
將復(fù)選框值存儲(chǔ)在具有唯一ID的單獨(dú)表中,將復(fù)選框中的id值設(shè)置為值,并將字段名稱(chēng)顯示為標(biāo)簽
因?yàn)閷?lái)在某些情況下必須更改復(fù)選框名稱(chēng),以便可以輕松更改該特定 ID 行數(shù)據(jù)。當(dāng)您嘗試使用逗號(hào)分隔符保存時(shí),這是不可能的。

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊
在這里,我找到了一種方法來(lái)做到這一點(diǎn)。希望它能幫助別人。謝謝
控制器
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>
函數(shù) 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 關(guān)注
- 96 瀏覽
添加回答
舉報(bào)