如果存在不必要的列,如何存储编辑的数据

时间:2017-03-12 06:36:02

标签: php codeigniter

这是产品数据编辑表单。其中,根据产品ID选择大小(输入名称=“大小”)和颜色(输入名称=“颜色”)。如果我想删除所有颜色选择或想要添加新颜色,如果输入不存在则如何进行查询然后忽略。因为输入颜色,大小和其他图像不是固定输入而是jquery的结果。

预览: dsadasd

我的观点:

<div id="ukuna" class="tab-pane fade">
  <?php echo br(2)?>
  <div class="row">
  <div class="col-md-6 input group">
  <a href="javascript:void(0);" class="btn btn-success tambah_ukuran_edit">add size</a><?php echo br(2)?>
        <label>Opsional size:</label>
        <div id="get_sizing">
           <?php foreach($get_sizex as $data){?>
          <div id="get_dropx">
            <a href="javscript:void(0)" onclick="hapus_ukuran_dipilih(this)" data-id="<?php echo $data['id_size']?>" class="label label-danger">X</a>
            <select class="form-control" name="size_update[]" id="sizing" style="margin-bottom:10px;">               
            <?php foreach($get_data_sizex_all as $datac){
              if($datac['id_opsi_size'] == $data['id_opsi_get_size']){?>
                <option value="<?php echo $datac['id_opsi_size']?>" SELECTED><?php echo $datac['opsi_size']?></option>
                <?php }else{?>
                <option value="<?php echo $datac['id_opsi_size']?>"><?php echo $datac['opsi_size']?></option>
                <?php }}?>
            </select>
          </div>
        <?php }?>
        </div>
    </div>

    <div class="col-md-6 input group">
    <a href="javascript:void(0);" class="btn btn-success tambah_warna_edit">add color</a><?php echo br(2)?>
        <label>Opsional Color:</label>
        <div id="get_coloring">
        <?php foreach($get_colorx as $data){?>
          <div id="get_drop">
            <a href="javscript:void(0)" onclick="hapus_warna_dipilih(this)" data-id="<?php echo $data['id_color']?>" class="label label-danger">X</a>
            <select class="form-control" name="color_update[]" id="coloring" style="margin-bottom:10px;">               
            <?php foreach($get_data_colorx_all as $datac){
              if($datac['id_opsi_color'] == $data['id_opsi_get_color']){?>
                <option value="<?php echo $datac['id_opsi_color']?>" SELECTED><?php echo $datac['opsi_color']?></option>
                <?php }else{?>
                <option value="<?php echo $datac['id_opsi_color']?>"><?php echo $datac['opsi_color']?></option>
                <?php }}?>
            </select>
          </div>
        <?php }?>
        </div>
    </div>
</div>
</div>

<div class="hidden">  
<div id="get_color">
  <label onclick="remove_element_color()" class="label label-danger">X</label>
  <select class="form-control" name="color[]" id="coloring" style="margin-bottom:10px;">
    <?php foreach($get_data_colorx_all as $data){ ?>
      <option value="<?php echo $data['id_opsi_color']?>"><?php echo $data['opsi_color']?></option>
    <?php }?>
  </select>
</div>
<div id="get_size">
  <label onclick="remove_element_size()" class="label label-danger">X</label>
  <select class="form-control" name="size[]" id="sizing" style="margin-bottom:10px;">
    <?php foreach($get_data_sizex_all as $data){ ?>
      <option value="<?php echo $data['id_opsi_size']?>"><?php echo $data['opsi_size']?></option>
    <?php }?>
  </select>
  </div>
</div>

<script>
  $(".tambah_ukuran_edit").click(function(e){
    e.preventDefault();
   var select = document.getElementById( 'get_size' ).cloneNode( true );
   document.getElementById( 'get_sizing' ).appendChild( select );
});
$(".tambah_warna_edit").click(function(e){
    e.preventDefault();
   var select = document.getElementById( 'get_color' ).cloneNode( true );
   document.getElementById( 'get_coloring' ).appendChild( select );
});
</script>

我的控制器

function update_produk(){
    $config['upload_path']          = 'assets/img/produk';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $config['max_size']             = 300;
    $config['overwrite']            = TRUE;
    //$config['maintain_ratio']         = TRUE;
    //$config['create_thumb']           = TRUE;
    //$config['max_width']            = 75;
    //$config['max_height']           = 50;
    $this->load->library('upload', $config);

        $result=array();
        $files = $_FILES;
        $count = count($_FILES['gambar_tambah']['name']);
        for($i=0; $i<$count; $i++)
            {
            $_FILES['gambar_tambah']['name']= $files['gambar_tambah']['name'][$i];
            $_FILES['gambar_tambah']['type']= $files['gambar_tambah']['type'][$i];
            $_FILES['gambar_tambah']['tmp_name']= $files['gambar_tambah']['tmp_name'][$i];
            $_FILES['gambar_tambah']['error']= $files['gambar_tambah']['error'][$i];
            $_FILES['gambar_tambah']['size']= $files['gambar_tambah']['size'][$i];
            $this->upload->do_upload('gambar_tambah');
            $upload_data = $this->upload->data();
            $name_array[] = $upload_data['file_name'];
            $fileName = $upload_data['file_name'];
            $images[] = $fileName;
            $result[] = $files['gambar_tambah']['name'][$i];
            }

    $id = $this->input->post('id_produk');
    $target = $this->input->post('nama');
    if(empty($_FILES['gambar']['name'])){
                $data = $this->input->post();
                $data['id'] = $this->data['id'];
                $process = $this->produk_adm->update_produk_without_image($id,$data,$result);
                log_helper("produk", "Edit produk ".$target."");
                //redirect('trueaccon2194/produk');
    }else{
                $this->upload->do_upload('gambar');
                $gambar = $_FILES['gambar']['name'];
                $data = $this->input->post();
                $data['id'] = $this->data['id'];
                $gb_utama_lawas = $this->input->post('gb_utama');
                unlink('assets/img/produk/'.$gb_utama_lawas);
                $proses = $this->produk_adm->update_produk_with_image($id,$data,$gambar,$result);
                log_helper("produk", "Edit produk ".$target."");
                //redirect('trueaccon2194/produk');
    } 
}

和我的模特:

function update_produk_without_image($id,$data,$result){
    $data_produk = array(
        'nama_produk'   => $data['nama'],
        'slug'          => $data['slug'],
        'milik'         => $data['miliknya'],
        'jenis'         => $data['jenisnya'],
        'artikel'       => $data['artikel'],
        'merk'          => $data['merknya'],
        'keterangan'    => $data['editor1'],
        'tags'          => $data['tags'],
        'kategori'      => $data['kategori'],
        'parent'        => $data['parent'],
        'harga_retail'  => $data['retail'] - ($data['diskon'] / 100 * $data['retail']),
        'harga_odv'     => $data['odv'],
        'harga_net'     => $data['retail'] - ($data['diskon'] / 100 * $data['retail']),
        'diskon'        => $data['diskon'],
        'stok'          => $data['stok'],
        'berat'         => $data['berat'],
        'point'         => $data['point'],
        'status'        => $data['aktif'],
        'diubah'        => $data['id'],
        'tgl_diubah'    => date('Y-m-d H:i:s'),
    );
    $this->db->where('id_produk', $id);
    $this->db->update('produk', $data_produk);

    //$last_insert_id = $this->db->insert_id();


    if(empty($_POST['size_update'])){
        return true;
    }else{
    $count = count($_POST['size_update']);
    for($i=0; $i<$count; $i++) {
        $data_size = array(
        'id_opsi_get_size' => $data['size_update'][$i],
        );
        $this->db->where('id_produk',$id);
        $this->db->update('produk_get_size', $data_size);
        }
    }

    if(empty($_POST['size'])){
        return true;
    }else{
    $count = count($_POST['size']);
    for($i=0; $i<$count; $i++) {
        $data_size = array(
        'id_opsi_get_size' => $data['size'][$i],
        );
        $this->db->where('id_produk',$id);
        $this->db->insert('produk_get_size', $data_size);
        }
    }

    if(empty($_POST['color_update'])){
        return true;
    }else{
    $count = count($_POST['color_update']);
    for($i=0; $i<$count; $i++) {
        $data_color = array(
        'id_opsi_get_color' => $data['color_update'][$i],
        );
        $this->db->where('id_produk',$id);
        $this->db->update('produk_get_color', $data_color);
        }
    }

    if(empty($_POST['color'])){
        return true;
    }else{
    $count = count($_POST['color']);
    for($i=0; $i<$count; $i++) {
        $data_color = array(
        'id_opsi_get_color' => $data['color'][$i],
        );
        $this->db->where('id_produk',$id);
        $this->db->insert('produk_get_color', $data_color);
        }
    }

    $count = count($result);
    for($i=0; $i<$count; $i++){
    $data_add = array(
        'gambar' => $result[$i],
    );
    $this->db->where('id_produk',$id);
    $this->db->update('produk_image', $data_add);
    //$result[]=$this->db->insert_id();
    }
}

请帮助我,或者我的问题的其他解决方案,谢谢

0 个答案:

没有答案