codeigniter多个复选框更新了用户ID

时间:2019-01-22 01:03:57

标签: php codeigniter

我想问的是,我的问题是如何以表中复选框的形式更新所有数据,并使用“ true” text = checkbox on /“ false” =复选框=关闭,显示内容和代码如下

显示:

enter image description here

红色圆圈按钮可更新表格中的所有数据

class setting_c扩展了CI_Controller {

function __construct() {
    parent::__construct();

    $this->load->model('user_model');
    //validasi jika user belum login

}
public function index() {
  $this->load->model('user_model');
    $data['user_data'] = $this->user_model->getall();
    $this->load->view('pages/setting', $data);
}

 public function save_edit(){

    if (isset($_POST['btn-edit'])) {


        $ur  = $this->input->post('ckls');
        $ad  = $this->input->post('ad[]');
        $ed  = $this->input->post('ed[]');
        $dd  = $this->input->post('dd[]');
        $xd  = $this->input->post('ao[]');
        $id  = $this->input->post('eo[]');
        $pd  = $this->input->post('do[]');
        $ao  = $this->input->post('xd[]');
        $eo  = $this->input->post('id[]');
        $do  = $this->input->post('pd[]');


        $jml_username = count($ur);
        for ($i=0;$i<$jml_username;$i++){
        if(isset($ad[$i])){
            $ad[$i] = "True";
        }else{
            $ad[$i] = "False";
        };

        if(isset($ed[$i])){
            $ed[$i] = "True";
        }else{
            $ed[$i] = "False";
        };

        if(isset($dd[$i])){
            $dd[$i] = "True";
        }else{
            $dd[$i] = "False";
        };
        if(isset($ao[$i])){
            $ao[$i] = "True";
        }else{
            $ao[$i] = "False";
        };

        if(isset($eo[$i])){
            $eo[$i] = "True";
        }else{
            $eo[$i] = "False";
        };

       if(isset($do[$i])){
            $do[$i] = "True";
        }else{
            $do[$i] = "False";
        };

        if(isset($xd[$i])){
            $xd[$i] = "True";
        }else{
            $xd[$i] = "False";
        };

        if(isset($id[$i])){
            $id[$i] = "True";
        }else{
            $id[$i] = "False";
        };

        if(isset($pd[$i])){
            $pd[$i] = "True";
        }else{
            $pd[$i] = "False";
        };


        $data[$i] = array(
                'AddData' => $ad[$i],
                'EditData' => $ed[$i],
                'DeleteData' =>  $dd[$i],
                'AddOcc' =>  $ao[$i],
                'EditOcc' =>  $eo[$i],
                'DeleteOcc' =>  $do[$i],
                'ExportData' =>  $xd[$i],
                'ImportData' =>  $id[$i],
                'PrintData' =>  $pd[$i]
            );
        $this->db->where('Username', $ur[$i]);
        $this->db->update('utililas_telkom', $data[$i]);  
        }

        $this->session->set_flashdata('msg', $ad1);
        redirect('setting_c');
    }else{
        redirect('setting_c');
    }

 }

}

视图

 <div class="box">
        <div class="box-header">
          <h3 class="box-title">Data </h3>
        </div>
        <!-- /.box-header -->
        <div class="box-body">
          <?php echo $this->session->flashdata('msg');?>
         <form method="post" action="<?php echo base_url('setting_c/save_edit') ?>" id="form-delete">
          <table id="search1" class="table table-striped table-bordered">
            <thead>
            <tr>
              <th>Select</th>
              <th>User Name</th>
              <th>Add Data</th>
              <th>Edit Data</th>
              <th>Delete Data</th>
              <th>Add OCC</th>
              <th>Edit OCC</th>
              <th>Delete OCC</th>
              <th>ExportData</th>
              <th>ImportData</th>
              <th>PrintData</th>
              <th>Action</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($user_data->result_array() as $i):
              $username = $i['Username'];
              $ad = $i['AddData'];
              $ed = $i['EditData'];
              $dd = $i['DeleteData'];
              $ao = $i['AddOcc'];
              $eo = $i['EditOcc'];
              $do = $i['DeleteOcc'];
              $xd = $i['ExportData'];
              $id = $i['ImportData'];
              $pd = $i['PrintData'];
            ?>
          <tr>  
              <td><input type="checkbox" name="ckls[]"  value="<?php echo $username; ?>"></td>         
              <td><?php echo $username; ?></td>
              <td><input type="checkbox" name="ad[]" <?php echo ($ad=='True'||$ad=='true')?"checked":"" ;?> ></td>
              <td><input type="checkbox" name="ed[]" <?php echo ($ed=='True'||$ed=='true')?"checked":"" ;?> > </td>
              <td><input type="checkbox" name="dd[]" <?php echo ($dd=='True'||$dd=='true')?"checked":"" ;?> > </td>
              <td><input type="checkbox" name="ao[]" <?php echo ($ao=='True'||$ao=='true')?"checked":"" ;?> > </td>
              <td><input type="checkbox" name="eo[]" <?php echo ($eo=='True'||$eo=='true')?"checked":"" ;?> > </td>
              <td><input type="checkbox" name="do[]" <?php echo ($do=='True'||$do=='true')?"checked":"" ;?> > </td>
              <td><input type="checkbox" name="xd[]" <?php echo ($xd=='True'||$xd=='true')?"checked":"" ;?> > </td>
              <td><input type="checkbox" name="id[]" <?php echo ($id=='True'||$id=='true')?"checked":"" ;?> > </td>
              <td><input type="checkbox" name="pd[]" <?php echo ($pd=='True'||$pd=='true')?"checked":"" ;?> > </td>
          </tr>


            <?php  endforeach;?>
            </tbody>

          </table>
           <div class="col-xs-4">
              <button type="submit" class="btn btn-primary btn-block btn-flat" name="btn-edit">Save Data</button>

            </div>
            <div class="col-xs-4">
              <a href="<?php echo base_url('setting_c'); ?>" class="btn btn-primary btn-block btn-flat" >Reload Data
              </a>
            </div>
            <div class="col-xs-4">
              <button  class="btn btn-primary btn-block btn-flat">Delete</button>
            </div>
          </form>
        </div>
        <!-- /.box-body -->
      </div>
  <!-- /.row -->

</section>
<!-- /.content -->

  

数据库

enter image description here

请帮助,谢谢

0 个答案:

没有答案