类别与每个类别的兼容性,带有代码符号中的复选框

我有一类化学品,需要检查每类化学品与自我和他人的兼容性。带有复选框的循环内的循环。1. 无法通过javascript点击隐藏元素时设置值;2.我如何继续和优化,因为24个类别的数据高达14000 +奇数条目。3. 建议使用逗号分隔值或单个条目?4. 如何编辑记录并保持复选框选中时从同一页面上的数据库表中提取数据


代码如下:


控制器


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->_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;

}

视图


<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>                           

http://img2.mukewang.com/631307c60001689c18090742.jpg

SMILET
浏览 75回答 2
2回答

斯蒂芬大帝

将复选框值存储在具有唯一ID的单独表中,将复选框中的id值设置为值,并将字段名称显示为标签因为将来在某些情况下必须更改复选框名称,以便可以轻松更改该特定 ID 行数据。当您尝试使用逗号分隔符保存时,这是不可能的。

泛舟湖上清波郎朗

在这里,我找到了一种方法来做到这一点。希望它能帮助别人。谢谢控制器public function compactibility_chart_master(){&nbsp; &nbsp; $data['title'] = 'Compactibility Chart Master';&nbsp; &nbsp; $this->form_validation->set_rules('category[]', 'Category Name','required|trim|xss_clean');&nbsp; &nbsp; if($this->form_validation->run() === FALSE){&nbsp; &nbsp; &nbsp; &nbsp; $data['category'] = $this->admin_model->get_all_active_entities('tbl_category');&nbsp; &nbsp; &nbsp; &nbsp; $this->load->view('admin/compactibility_chart_master',$data);&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; $this->admin_model->set_compactibility_chart_master();&nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_flashdata('success','Compacitibiliy Chart Master updated Successfully');&nbsp; &nbsp; &nbsp; &nbsp; redirect('compactibility_chart_master', 'refresh');&nbsp; &nbsp; }}型public function set_compactibility_chart_master(){&nbsp; &nbsp; $post = $this->input->post();&nbsp; &nbsp; for($z=0;$z<count($post['category']);$z++){&nbsp; &nbsp; &nbsp; &nbsp; $sub_cat = "";&nbsp; &nbsp; &nbsp; &nbsp; foreach($this->input->post('catcat'. $z) as $values){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $sub_cat .= $values.",";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $exp = explode(",",$sub_cat);&nbsp; &nbsp; &nbsp; &nbsp; $newexp = array_unique($exp);&nbsp; &nbsp; &nbsp; &nbsp; $newexp = implode($newexp, ',');&nbsp; &nbsp; &nbsp; &nbsp; $insertdata = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'category'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => $post['category'][$z],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'category_compact'&nbsp; => $newexp,&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $this->db->where('category',$post['category'][$z]);&nbsp; &nbsp; &nbsp; &nbsp; $exist = $this->db->get('tbl_compactibility_master');&nbsp; &nbsp; &nbsp; &nbsp; $exist = $exist->num_rows();&nbsp; &nbsp; &nbsp; &nbsp; if($exist == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->db->insert('tbl_compactibility_master',$insertdata);&nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->db->where('category',$post['category'][$z]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->db->update('tbl_compactibility_master',$insertdata);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return true;}视图<form name="compatibility_form" id="compatibility_form" method="post"&nbsp; enctype="multipart/form-data" data-parsley-validate>&nbsp; &nbsp; <table class="table table-bordered" width="100%">&nbsp; &nbsp; &nbsp; &nbsp; <?php $cc=0; foreach($category as $cat_results){ ?>&nbsp; &nbsp; &nbsp; &nbsp; <tr class="data">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary btn-sm my-2" type="button"><?=$cat_results->category_name;?></button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="hidden" name="category[]" class="categooryclass" value="<?=$cat_results->id;?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php foreach($category as $key => $cat_res){ $get_single = $this->admin_model->get_single_record_compactibility($cat_results->id);?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="form-check-inline">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label class="form-check-label">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <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;?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<label class="form-check-label"><?=$cat_res->category_name;?></label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>&nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp; &nbsp; &nbsp;<?php $cc++; } ?>&nbsp; &nbsp;</table>&nbsp; &nbsp;<div class="row">&nbsp; &nbsp; &nbsp; &nbsp;<div class="col-md-4">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="btn btn-primary btn-sm my-4" id="submit_button" type="submit">Save Data</button>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></form>函数 get_single_record_compactibility()public function get_single_record_compactibility($cat){&nbsp; &nbsp; $this->db->where('category',$cat);&nbsp; &nbsp; $result = $this->db->get('tbl_compactibility_master');&nbsp; &nbsp; $result = $result->row();&nbsp; &nbsp; if(!empty($result)){&nbsp; &nbsp; &nbsp; &nbsp; return explode(",",$result->category_compact);&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; return array();&nbsp; &nbsp; }}脚本$('.checkedme').on("click",function() {&nbsp; &nbsp; var getVal = $(this).val();&nbsp; &nbsp; var test = $(this).data('forcheck');&nbsp; &nbsp; $('#company_form').find("input[data-forcheck='"+getVal+"'][value='"+test+"']").prop('checked',this.checked);});
打开App,查看更多内容
随时随地看视频慕课网APP