user3352042
user3352042

Reputation: 131

Edit and Delete action with Codeigniter

i want to update and delete an entry im really new at this and i dont know where to put the function..so yeah i need help

controller

function delete()
{
    $this->site_model->delete_row('booking');
    $this->view();
}
}

site_model

function delete_row()
{
    $this->db->where('id', $this->uri->segment(3));
    $this->db->delete('booking');
}

and my view

<?php 
$this->table->set_heading("Name","Nationality","Number of Guest","Date","Package","Other      Request","Delete Record");
$qry = $this->db->get('booking');
foreach ($qry->result() as $row) {
$this->table->add_row($row->name,$row->nationality,$row->number_of_guest,$row->date,$row->package,$row->request);

}
echo $this->table->generate();

 ?>

TIA, im really new at this so please consider this..

Upvotes: 1

Views: 5579

Answers (2)

Shomz
Shomz

Reputation: 37701

Since you have no real question, I'll just give you a few hints before we close this.

  • database communication should happen in the model, not the view
  • I suggest you use ids instead of uri segments, especially when you're deleting something (see @Wlises' answer)
  • to load a view, use $this->load->view('view_name_here');
  • to create a delete button, you simply need to redirect browser to your delete method of your controller (something like: site.com/booking/delete/5, where 5 is the id of the booking) using a simple anchor, JavaScript redirection, form submit, etc...

The bottom line is: please use the tutorial, it's very good and covers 99% of what you'd need: http://ellislab.com/codeigniter/user-guide/tutorial/

UPDATE

You can build your own table for more flexibility (and you're probably more comfortable with HTML rather than CI's helper functions). So, every row would need a delete button/link like this:

<tr>
    <td><?php echo $row->name; ?></td>
    ... etc
    <td><a href="<?php echo base_url('booking/delete/' . $row->id); ?>">DELETE</a></td>
</tr>

Upvotes: 0

wrivas
wrivas

Reputation: 509

Try this:

You can pass parameters through the URL.. so, for example:

yoursite/index.php/controller/delete/12

The ID = 12

In your controller:

function delete($id=null)
{
    $this->booking_model->delete($id);
    $this->view('my_view_name');
}

booking_model

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

Upvotes: 1

Related Questions