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
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.
$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.
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().
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.
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.
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
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 ();
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.
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']);
}
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With