Welcome Guest, Not a member yet? Register   Sign In
Passing query result back to controller / view...
#1

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
Reply
#2

I'm a little unsure as to where I put $this->db->affected_rows() :/
Reply
#3

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....'?>
Reply
#4

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.
Rolleyes
Reply




Theme © iAndrew 2016 - Forum software by © MyBB