如何使用一个按钮Codeigniter更新多行复选框

时间:2018-01-22 02:53:39

标签: php codeigniter checkbox

嘿伙计们,你们可以帮我解决这个复选框问题吗?如果我检查列并按更新,我希望更新在当前时间发生。

this is an example

我想如果选中复选框并按更新按钮,列将在当前时间更新

这是我的观点:

<div class="container">      
  <div class="row">
    <div class="col-md-10">
      <div class="panel panel-info">
        <div class="panel-heading">Setting Jam & Tanggal Mesin</div>
        <div class="panel-body">
              <table id="devid_auto" class="table table-bordered dt-responsive" cellspacing="0" width="100%">
                <thead>
                  <tr>
                    <th width="1%" align="center"><input type="checkbox" onClick="toggle(this)" id="checkAll" name="checkAll" /></th>
                    <th width="10%" align="center">Nama Mesin</th>
                    <th width="5%" align="center">IP Address</th>
                    <th width="10%" align="center">SN</th>
                    <th width="5%" align="center">Ethernet Port</th>
                    <th width="10%" align="center">Last Update</th>
                  </tr>
                </thead>
                <tbody>
                <?php 

                foreach($data as $d){

                ?>
                  <tr>
                    <td><input class="childbox" type="checkbox" name="msg[]" id="id" value="<?php echo $d['devid_auto'] ?>" /></td>
                    <td class="data-check"  ><?php echo $d['device_name']; ?></td>
                    <td class="data-check"  ><?php echo $d['ip_address']; ?></td>
                    <td class="data-check"  ><?php echo $d['sn']; ?></td>
                    <td class="data-check"  ><?php echo $d['ethernet_port']; ?></td>
                    <td class="data-check"  ><?php echo $d['lastupdate_date']; ?></td>
                  </tr>
                <?php } ?>
                </tbody>                    
              </table>

              <div class="form-group">
                <label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">Tanggal & Waktu Sekarang</label>
                <div class="col-sm-3">
                  <input type="" class="form-control" id="" value="<?php date_default_timezone_set("Asia/Jakarta"); echo date(" Y-m-d H:i:s ");?>""  readonly>
                </div>  
              </div>
        </div>
        <div class="panel-footer text-right">
          <button allign="center" class="btn btn-danger" onclick="update()">Update Date and time</button>
        </div>
      </div>
      <script src="<?php echo base_url() ?>assets/baru/jquery-3.2.1.min.js"></script>
      <script src="<?php echo base_url() ?>assets/baru/jquery.dataTables.min.js"></script>
      <script src="<?php echo base_url() ?>assets/baru/dataTables.bootstrap4.min.js"></script>
      <script type="text/javascript">
          $(document).ready( function () 
          {

              $('#devid_auto').DataTable();

              $("input[name='checkAll']").click(function() {
                    var checked = $(this).attr("checked");
                    $("#devid_auto tr td input:checkbox").attr("checked", checked); });

          });

          function toggle(id) 
          { 
            checkboxes = document.getElementsByName('msg[]'); 
            for(var i=0, n=checkboxes.length;i<n;i++) { 
              checkboxes[i].checked = id.checked; 
            } 
          }

          function update(id)
          {
              var list_id = [];
              $("#id:checked").each(function() {
                      list_id.push(parseInt(this.value));
              });
          console.info(JSON.stringify(list_id));

              if(list_id.length > 0)
              {
                  if(confirm('Are you sure update this '+list_id.length+' data?'))
                  {
                      $.ajax({
                          type: 'POST',
                          data: {'devid_auto': list_id},
                          url: '<?php echo site_url('setting/mesin_update')?>',
                          success: function(result)
                          {
                              var hasil = result.replace(/\s/g,'');
                              if(hasil == 'y')
                              {
                                alert("Data Berhasil di Update");
                                location.reload();
                              }
                              else
                              {
                                  alert('Failed.');
                              }

                          },
                          error: function (jqXHR, textStatus, errorThrown)
                          {
                              alert('Error update data');
                          }
                      });
                  }
              }
              else
              {
                  alert('no data selected');
              }

          }
            </script>

    </div><!-- end cols-->
  </div><!--end row-->
</div> <!-- end container-->    

这是我的控制者:

public function mesin_update()
    {
        $initiated_date =  date('Y-m-d H:i:s');
        $data = array(
                'lastupdate_date' => $initiated_date,
            );
        $devids = $this->input->post('devid_auto', True);
        foreach($devids as $devid){
           $this->Jam_tgl_model->jam_tanggal_update(array('devid_auto' => $devid ), $data);
        }
        echo 'y';
    }

这是我的模特:

public function jam_tanggal_update($where, $data)
{
    $this->db->update($this->table, $data, $where);
    return $this->db->affected_rows();
}

我已经尝试使用我的代码但是列最后更新日期没有任何改变

请帮助修复我的代码,以便我可以完成它

(新编辑):   - 这是结果:   - I checked the line   - and pressing the update button, can you change the script function update (id)?   - and this is the result after I hit update button

3 个答案:

答案 0 :(得分:0)

模型

中尝试此操作
public function jam_tanggal_update($where, $data) {
    $this->db->where($where);
    $this->db->update($this->table_dept, $data);
    return $this->db->affected_rows();
} 

有关更新的更多详细信息:See CodeIgniter official documentation

答案 1 :(得分:0)

在contorller中

$devids = $this->input->post('devid_auto', True);
foreach($devids as $devid){
   $this->Jam_tgl_model->jam_tanggal_update(array('devid_auto' => $devid), $data);
}
  

注意:您也可以使用ci $ this-&gt; db-&gt; update_batch()

答案 2 :(得分:0)

试试这个..这可能会帮到你。 控制器

    public function delete_inbox()
    {       
         $ids = ( explode( ',', $this->input->get_post('devid_auto') )); 
  $initiated_date =  date('Y-m-d H:i:s');
        $data = array(
                'lastupdate_date' => $initiated_date,
            );
          $del= $this->model-mesin_update($ids,$data);      
    }   

型号:

public function jam_tanggal_update($where, $data){

       $ids = $ids;
       $count = 0;
        foreach ($ids as $id){
       $did = intval($id);
        $this->db->where('id', $did);
        $res=$this->db->update('<table_name',$data);
        $count = $count+1;
      }


  }