更新复选框Codeigniter

时间:2019-02-10 06:32:45

标签: php codeigniter checkbox

我的控制器:

15:45

我的观点:

public function update($id)
{
    $data['tag'] = $this->Tag_model->get_all();
    $data['gambar'] = $this->Gambar_model->get_by_id($id);
    $this->load->view('gambar/gambar_edit', $data);
}

public function update_action()
{
    $this->_rules();

    if ($this->form_validation->run() == FALSE) {
        $this->update($this->input->post('id_gambar'));
    } else {
        $data = array(
            'judul_gambar' => $this->input->post('judul_gambar', TRUE),
            'judul_tag' => implode(', ', $this->input->post('judul_tag', TRUE)),
            'img_gambar' => $this->input->post('img_gambar', TRUE)
         );

         $this->Gambar_model->update($this->input->post('id_gambar'), $data);
         $this->session->set_flashdata('flash', 'diubah');
         redirect('gambar');
    }
}

我想问一下如何进行更新时如何检查我提交的复选框。更新功能正在运行,但我只是在输入文本上获得了值,而不是选中的复选框,因此当我执行更新时,该复选框为空值,必须再次进行检查。我当时在搜索它,它必须是爆炸值,但我不知道它应该在哪里。 非常感谢您的帮助,谢谢

1 个答案:

答案 0 :(得分:1)

在更新行时,必须先使用explode(),而要使用implode()。之后,使用in_array()查找行

有关更多详细信息。

in_array() explode()

<?= form_open('gambar/update_action') ?>
  <div class="form-group col-lg-8">
    <input type="hidden" class="form-control" id="idgambar" name="id_gambar" value="<?= $gambar['id_gambar'] ?>">
    <label for="judulgambar">Nama</label>
    <input type="text" class="form-control" id="judulgambar" name="judul_gambar" value="<?= $gambar['judul_gambar'] ?>">
    <small class="form-text text-danger font-italic"><?= form_error('judul_gambar') ?></small>
  </div>
  <div class="form-group col-lg-8">
    <label for="imggambar">Gambar</label>
    <input type="text" class="form-control" id="imggambar" name="img_gambar" value="<?= $gambar['img_gambar'] ?>">
    <small class="form-text text-danger font-italic"><?= form_error('img_gambar') ?></small>
  </div>
  <div class="form-group pl-3">
    <?php 
    foreach ($tag as $row): 
      $explodeRow = explode(',',$row['judul_tag']);
    ?>
      <div class="form-check-inline">
          <input class="form-check-input" type="checkbox" name="judul_tag[]" <?php if(in_array($row['judul_tag'],$explodeRow)) echo 'checked'; ?> value="<?= $row['judul_tag'] ?>">
          <label class="form-check-label" for="gridCheck">
            <?= $row['judul_tag'] ?>
          </label>
      </div>
    <?php endforeach; ?>
    <small class="form-text text-danger font-italic"><?= form_error('judul_tag[]') ?></small>
  </div>
  <button type="submit" class="btn btn-success ml-3" name="submit">Ubah</button>
<?= form_close() ?>