0

Please bear with me as I am new to CodeIgniter

I'll try to explain by giving an example

Let's say I have a controller called news and the urls /news/create and /news/delete can delete news.

Problem is that I have comments on the news page that a user should be able to delete. But right now if you /comment/delete/1 it will remain on the /comment/delete page but I want to user to remain on the specific news page.

Right now I use this:

public function delete($id = NULL)
{
    $this->news_model->delete_news($id);
    redirect('/news/');
}

But this doesn't not take into account to go back to a specific news item.

Kindly let me know how I can tackle this issue.

2 Answers2

0

You will simply need to reference the News id. Assuming /news/n/ is a valid url.

public function delete($id = NULL, $newsId)
{
    $this->news_model->delete_news($id);
    redirect('/news/' . $newsId);
}
mrhn
  • 17,961
  • 4
  • 27
  • 46
  • Should I create a controller for comments? How would I go about deleting a comment from a news page and remaining on that news page? – Roël Gonzalez Nov 09 '15 at 01:31
  • No, i think the best way is to allow for a second parameter in the routes http://stackoverflow.com/questions/13246286/how-to-route-2-parameters-to-a-controller and create a delete method similar to mine to get the wanted logic :) – mrhn Nov 09 '15 at 01:35
0

Add this in your view

<a href="<?php echo base_url() ?>Controller_name/delete/<?php echo ($id /* as your code */) ?>" >delete</a>

So when you click that link it will call the controller with the ID.

so in controller

In Controller

public function delete($id)
{
    $result = $this->news_model->delete_news($id);
    if($result == TRUE){
        redirect('news'); # this will call index
    }
    else{
        echo 'Delete Error';
   }
}

In Model

public function delete_news($id)
{
    # Change table name as well
    if (!$this->db->delete('table_name', array('id' => $id))) {
        # Error
        return FALSE;
    }
    else{
        # Success
        return TRUE;
    }
}

EDIT 01

public function delete($id)
{
    $result = $this->news_model->delete_news($id);
    if($result == TRUE){
        $data['data'] = $this->model_name->data_method();
        $this->load->view("View_name");
    }
    else{
        echo 'Delete Error';
   }
}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85