Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 872
Abhijit Avatar asked Oct 31 '13 07:10

Abhijit


People also ask

How to delete data in CodeIgniter 4?

CodeIgniter 4 Model delete() methodTakes a primary key value as the first parameter and deletes the matching record from the model's table: If the model's $useSoftDeletes value is true, this will update the row to set deleted_at to the current date and time.

How can check rows affected in CodeIgniter?

$this->db->affected_rows() Displays the number of affected rows, when doing "write" type queries (insert, update, etc.). Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows.

How to delete an existing record from a table in CodeIgniter?

In CodeIgniter, delete() method is used to delete an existing record from database table. $table(mixed):- The table(s) to delete from. Delete Record using $this->db->delete() This is how you can delete a record from ’employee_master’ table using $this->db->delete().

What is the difference between truncate () and delete () in CodeIgniter?

The Codeigniter Delete function is used to delete the one or more row data from database table. The truncate () method of codeigniter is used to remove all records from a database table. It performs the same function as a DELETE statement without a WHERE clause. My name is Devendra Dode.

What is active record in CodeIgniter?

Active record is a design pattern that makes it easy to interact with the database in ease, secure and eloquent way. Allows you to work with multiple database engines such as MySQL, SQL Server, etc. without rewriting the application code CodeIgniter uses drivers specific for each database engine in the background.

How to delete a record with the ID of 3?

We will delete the record with the id of 3. $this->db->delete (‘orders’); deletes the database row in orders table based on the criteria set using the where clause. To execute the above code, load the following URL in our web browser


4 Answers

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 ();

like image 158
Waseem shah Avatar answered Oct 16 '22 03:10

Waseem shah


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.

like image 41
Jeemusu Avatar answered Oct 16 '22 04:10

Jeemusu


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.

like image 42
Levin Avatar answered Oct 16 '22 03:10

Levin


**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;
    }
like image 39
Hemant Randive Avatar answered Oct 16 '22 04:10

Hemant Randive