[php] how to delete a specific row in codeigniter?

I am new in codeigniter.In my view page I am showing the data from database in a table where I have two anchor tags for update and delete. I want to delete a specific row from database through id.

my view page is

 <?php foreach($query as $row){ ?>
 <tr>
 <td><?php echo $row->name  ?></td>
  <td><?php echo $row->testi  ?></td>
  <td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
  <td><a href="#ajax-modal2" data-id="delete[]" role="button" class="Delete" data-toggle="modal" onClick="confirm_delete()"><i class="icon-trash"></i></a></td>
  </tr>

<?php } ?>
</table>

my controller page is

function delete_row()
{

   $this->load->model('mod1');
   $this->mod1->row_delete();
   redirect($_SERVER['HTTP_REFERER']);  
}

and my model page is

 function row_delete()
  {

      $this->db->where('id', $id);
      $this->db->delete('testimonials'); 
  }

I want to delete the row by catching the respective id. Please dont be harsh. thank you

This question is related to php codeigniter

The answer is


It will come in the url so you can get it by two ways. Fist one

<td><?php echo anchor('textarea/delete_row', 'DELETE', 'id="$row->id"'); ?></td>
$id = $this->input->get('id');

2nd one.

$id = $this->uri->segment(3);

But in the second method you have to count the no. of segments in the url that on which no. your id come. 2,3,4 etc. then you have to pass. then in the ();


a simple way:

in view(pass the id value):

<td><?php echo anchor('textarea/delete_row?id='.$row->id, 'DELETE', 'id="$row->id"'); ?></td>

in controller(receive the id):

$id = $this->input->get('id');
$this->load->model('mod1');
$this->mod1->row_delete($id);

in model(get the passed args):

function row_delete($id){}

Actually, you should use the ajax to POST the id value to controller and delete the row, not the GET.


**multiple delete not working**

function delete_selection() 
{
        $id_array = array();
        $selection = $this->input->post("selection", TRUE);
        $id_array = explode("|", $selection);

        foreach ($id_array as $item):
            if ($item != ''):
                //DELETE ROW
                $this->db->where('entry_id', $item);
                $this->db->delete('helpline_entry');
            endif;
        endforeach;
    }

My controller

public function delete_category()   //Created a controller class //
    {      
         $this->load->model('Managecat'); //Load model Managecat here 
         $id=$this->input->get('id');     //  get the requested in a variable
         $sql_del=$this->Managecat->deleteRecord($id); //send the parameter $id in Managecat  there I have created a function name deleteRecord

         if($sql_del){

               $data['success'] = "Category Have been deleted Successfully!!";  //success message goes here 

         }

    }

My Model

public function deleteRecord($id) {

    $this->db->where('cat_id', $id);
    $del=$this->db->delete('category');   
    return $del;

}

You are using an $id variable in your model, but your are plucking it from nowhere. You need to pass the $id variable from your controller to your model.

Controller

Lets pass the $id to the model via a parameter of the row_delete() method.

function delete_row()
{
   $this->load->model('mod1');

   // Pass the $id to the row_delete() method
   $this->mod1->row_delete($id);


   redirect($_SERVER['HTTP_REFERER']);  
}

Model

Add the $id to the Model methods parameters.

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

The problem now is that your passing the $id variable from your controller, but it's not declared anywhere in your controller.