douxin8383 2014-11-22 14:43
浏览 51

Codeigniter ::删除复选框中的多个记录

I can't seem to find what is wrong with my code. I want to be able to delete records which have been checked in a table.

This is my VIEW:

       <table id="table" name="table" class="table table-bordered table-condensed table-striped table-primary table-vertical-center checkboxs">
              <thead>
                <tr>
                  <th style="width: 1%;" class="uniformjs"><input type="checkbox" /></th>
                  <th class="center">Item Category</th>
                  <th class="center" style="width: 90px;">Actions</th>
                </tr>
              </thead>
              <?php foreach($categorycontent->result() as $row): ?>
                <tr>
                  <th style="width: 1%;" class="uniformjs"><input type="checkbox" name="delete[]" value="<?php echo $row->id; ?>"/></th>
                  <td class="center"><?php echo $row->category_name; ?></td>
                  <td class="center">
                    <a href="#" class="btn-action glyphicons pencil btn-success" title="Edit" onClick="popedit('<?php echo $row->id; ?>', '<?php echo base_url(); ?>category/update_category/', 'category')"><i></i></a>
                    </td>
                </tr>
              <?php endforeach; ?>
            </table>

<script type="text/javascript">
  $(function()
  {

    popadd("#newcategory", "<?php echo base_url(); ?>category/create_category/", 'category');
    popdeletechecked("#deletecategory", "Deleting this record/s will delete all linked information.</br>Are you sure you want to delete it?",  "<?php echo base_url(); ?>category/remove_category/");
  });
</script>

My ajax function which was called in the view:

function popdeletechecked(buttonid, msg, url)
{
  $(buttonid).click(function(){
    
    bootbox.confirm(msg,'No', 'Yes', function(result) {

      if(result) {
        $.ajax({
          url     : url,
          type    : 'POST',
          success : function(){
            window.location = url;
          }
        });
      }
      else {
      $.gritter.add({// Doesn't work on page reload
        title: 'Deleting Cancelled!'
      });
    }
  });
    
  });
}

My Controller:

public function remove_category()
{
    
    $checked = $this->input->post('delete');
    $this->category_model->delete_category($checked);
  
    redirect('category/read_category');

}

Model:

function delete_category($id)
{
  $this->db->where('id', $id);
  $this->db->delete('tblitemcategories');
}

There are no errors returned by firebug. I'm not sure what's wrong and I've tried changing my code based on the other questions of other users which were answered here in stack, but mine still doesn't work. Any help is much appreciated. I'm pretty new to Codeigniter and PHP. Thanks again in advance!

</div>
  • 写回答

1条回答 默认 最新

  • dpquu9206 2015-06-13 03:39
    关注

    You should use a foreach loop to delete. In the Controller:

    public function remove_category()
    {
    
        $checked = $this->input->post('delete');
        foreach($checked as $val){
            $this->category_model->delete_category($val);
        }
        redirect('category/read_category');
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错