CodeIgniter Forums

Full Version: Passing query result back to controller / view...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Helloooo,

So I have been following the tutorial in the user guide and am experimenting with them.

In my News controller I have..

PHP Code:
public function delete($id NULL)
    {
        if (
$id === NULL)
        {
                echo 
'Error - No Post ID...';
                
            }
            else 
            {
                
$this->news_model->delete_news($id);
        }
    } 

And my Model..

PHP Code:
public function delete_news($id)
        {
           
$query $this->db->delete('news', array('id' => $id)); 
        } 


So by visiting news/delete/[post id] I delete the post in question.

How to feed this back to my controller though?

I'd like to load a view if successfully and a different view if not.

I understand I can achieve this via an if statement in my controller, however how do I tell my controller the query was true/false?

Sorry for the (I'm sure painfully simple) question Big Grin
I'm a little unsure as to where I put $this->db->affected_rows() :/
OK, so I have a result - however I'd love someone to tell me if I'm doing it the 'correct' CI way.

I'm keen to be as strict as possible with my syntax...

Controller
PHP Code:
public function delete($id NULL)
        {
/*             if ($id === NULL)
            {
                    echo 'Error - No Post ID...';
                    
                }
                else 
                { */
                    
$data['result'] = $this->news_model->delete_news($id);
                    
$this->load->view('news/output'$data);
            
        } 

Model
PHP Code:
public function delete_news($id)
        {
            
$this->db->delete('news', array('id' => $id)); 
            return 
$this->db->affected_rows();
        } 

View
PHP Code:
<?php echo $result 'Post Deleted' 'Nothing Happened....'?>
You have to load the view like this:
$this->load->view('view_name');
after the if and else condition. Remember to load the view always once in the function.