Welcome Guest, Not a member yet? Register   Sign In
Weird non-object problem
#1

[eluser]Kemik[/eluser]
Hello,

I'm building a news article page. The article is shown, then comments and finally a form to submit your own comment.

After I submit a comment I receive the following error
Quote:Fatal error: Call to a member function on a non-object in /home/sh/public_html/project/system/application/views/news_article_view.php on line 1

If I comment out line 1 it gives the usual "cannot find $row variable message" but displays the article's comments and the "Your comment has been posted message" fine.

Controller
Code:
function article() // Displays individual article
    {
    
        $this->load->helper('date');
        
        $news_id = $this->uri->segment(3);
        
        if ($news_id == 0){
            show_error("No News ID passed");
        } else {
            $data['news_id'] = $news_id;
        }
        
        // Get news article
        $data['query'] = $this->news_model->get_news_article($news_id);
        
        // Get comments
        $data['query2'] = $this->news_model->get_comments($news_id);
                        
        // Prepare comment form
        $this->load->helper('form');
        $this->load->library('validation');
        
        $this->validation->set_error_delimiters('<p>', '</p>');
        
        $rules['comment']         = "trim|required|max_length[250]|xss_clean|strip_tags";    
        
        $this->validation->set_rules($rules);
        
        $fields['comment']            = 'comment';
    
        $this->validation->set_fields($fields);
        
        // Form submission        
        if ($this->validation->run() == TRUE) {
            
            $user_id = '1';

            /**
            *    if ($this->session->userdata('user_id')) {
            *        $user_id = $this->session->userdata('user_id');
            *    } else {
            *        show_error('User ID not found.');
            *    }
            */            
            
            // Add comment to the database
            $data['query'] = $this->news_model->add_comment($this->input->post('comment'), $user_id, $news_id, time());
            $data['commented'] = TRUE;
            
        }        
        
        $data['header'] = 'View Article';
        $data['page'] = 'news_article';
        $this->load->view('container', $data);
    }

Model
Code:
function get_news_article($news_id)
    {
        $query = "SELECT n.*, u.username, cat.name as category
                    FROM news_articles as n
                    INNER JOIN users as u
                        ON n.user_id = u.user_id
                    INNER JOIN news_categories as cat
                        ON n.category_id = cat.category_id
                    WHERE n.news_id = $news_id";
        
        return $this->db->query($query);
    }
    
    function get_comments($news_id)
    {
        $query = "SELECT c.*, u.username
                    FROM news_comments as c
                    INNER JOIN users as u
                        ON c.user_id = u.user_id
                    WHERE c.news_id = $news_id";
        
        return $this->db->query($query);
    }
    
    function add_comment($comment, $user, $news, $date)
    {
        $insert = array(
               'comment' => $comment ,
               'user_id' => $user ,
               'news_id' => $news ,
               'date_posted' => $date
            );

        $this->db->insert('news_comments', $insert);
    }

View
Code:
&lt;?php $row = $query->row(); ?&gt;

<h2>&lt;?=$row->title;?&gt;</h2>
&lt;?=$row->content;?&gt;
<p><i>&lt;?=$row->username;?&gt; - &lt;?=unix_to_human($row->date_posted, TRUE, 'eu');?&gt;</i></p>

<h2>Comments</h2>
&lt;?php foreach($query2->result() as $row2): ?&gt;

    <p><b>&lt;?=$row2->username;?&gt; - &lt;?=unix_to_human($row2->date_posted, TRUE, 'eu');?&gt;</b></p>
    <p>&lt;?=$row2->comment;?&gt;</p>
    
&lt;?php endforeach; ?&gt;

&lt;?php if (isset($commented) && $commented == TRUE): ?&gt;
    
<p>Your comment has been submitted.</p>  
    
&lt;?php else: ?&gt;

&lt;?php echo form_open("news/article/$news_id"); ?&gt;
<table border="0" cellpadding="3" cellspacing="1">

&lt;?php echo $this->validation->error_string; ?&gt;

<tr>
    <td>Comment</td>
    <td>&lt;textarea class="textarea" name="comment" cols="30" rows="5"&gt;&lt;?=$this->validation->comment;?&gt;&lt;/textarea&gt;</td>    
</tr>

</table>

&lt;input type="submit" class="submit" value="Add Comment" /&gt;

&lt;/form&gt;

&lt;?php endif; ?&gt;
#2

[eluser]Seppo[/eluser]
Well...
Code:
$data['query'] = $this->news_model->add_comment($this->input->post('comment'), $user_id, $news_id, time());
That´s seems to be the issue... add_comment is not returning a query result.
#3

[eluser]Kemik[/eluser]
Opps. Don't know why I added $data['query'] before that.

Thanks.
#4

[eluser]Tom Glover[/eluser]
Simple Mistake




Theme © iAndrew 2016 - Forum software by © MyBB