0

Please someone help me..i want to delete multiple items with check box selection in code igniter..

what i did: My view page looks like this

<script type="text/javascript" src="<?php echo base_url(); ?>/js/jquery_1_8_1_min.js"></script>
<script language="javascript">
$(function(){
    $("#selectall").click(function () {
        $('.case').attr('checked', this.checked);
    });
    $(".case").click(function(){
        if($(".case").length == $(".case:checked").length) {
            $("#selectall").attr("checked", "checked");
        } else {
            $("#selectall").removeAttr("checked");
        }
    });
});
</script>

<?php
foreach($query as $row)
{
    ?>
    <tr><td><input name="checkbox[]" class="case" type="checkbox"  value="<?php print $row->reg_id; ?>"></td></tr>
    <?php 
}
?>

My controller code:

function del($reg_id) //delete content
{
    $this->load->helper(array('form','url'));
    $this->load->model('registration');
    $query=$this->registration->delete($reg_id);
    $data['query']=$this->registration->cr_getall();
    $this->load->view('vw_register',$data);
}

Here my model:

public function del($reg_id)
{
    $this->load->database();
    $del=$this->db->delete('tbl_contctform',array('reg_id'=>$reg_id));
    if($del)
    {
        return $del;
    }
    else
    {
        return false;
    }
}

I know i done some mistakes.but i dont knw how to clear that.

is it possible to use ajax .?Please help me.

1
  • is the ques how to create model and controller ? rather than how to use checkbox to delete values ? Commented Sep 5, 2013 at 7:13

2 Answers 2

1

Try this,

$(".case").on('click',function(){
    var chk=$('.case:checked').length ? true : false;// if length is > 0 then return true
    $('#selectall').prop('checked',chk);// if chk true then check the selectAll checkbox         
});

Take a look on fiddle

You need to create a form submit and call the function of controller on form submit

# Your Controller Function
function del($reg_id) //delete content
{
    $this->load->helper(array('form','url'));
    $this->load->model('registration');
    $checkbox=$this->input->post('checkbox');# Using Form POST method you can use whatever you want like GET
    $reg_id=$this->input->post('reg_id');# Using Form POST method
    $regId_array=array();
    foreach($checkbox as $key=>$checked)
    {
        if($checked!==FALSE)
            $regId_array[]=$reg_id[$key];
    }
    if(!empty($regId_array))# array of checked reg_id to delete
    {
        $query=$this->registration->del($checkbox,$id);# use del here not delete as your model says
    }
    else{
        die('No checkbox selected!!!');
    }

    $data['query']=$this->registration->cr_getall();
    $this->load->view('vw_register',$data);
}

# Your Model Function
public function del($reg_id)
{
    $this->load->database();
    $this->db->where_in('reg_id', $reg_id);# delete all reg_id in $reg_id array variable
    $this->db->delete('tbl_contctform');

    if($del)
    {
        return $del;
    }
    else
    {
        return false;
    }
}

And your View should be like,

<?php
foreach($query as $row)
{
    ?>
    <tr><td>
        <input name="checkbox[]" class="case" type="checkbox" />
        <input name="reg_id[]" type="hidden"  value="<?php print $row->reg_id; ?>">
    </td></tr>
    <?php 
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I changed my View,Controller,Model like this..now it working properly

View:

<form name="forms" action="delete_checkbox/" method="post">
    foreach($query as $row)
    {?>
    <tr>
        <td><input type="checkbox" name="forms[]" id="forms[]"
                   value="<?php print $row->reg_id; ?>"/>
    </tr?
<?php } ?>

Controller:

function delete_checkbox() {
    $dat = $this->input->post('forms');
    for ($i = 0; $i < sizeof($dat); $i++) {
        print_r($dat[$i]);
        $this->load->model('registration');
        $this->registration->delete_check($dat[$i]);
    }
    redirect('viewreg/showall', 'refresh');
}

It works!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.