[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:
<?php $row = $query->row(); ?>
<h2><?=$row->title;?></h2>
<?=$row->content;?>
<p><i><?=$row->username;?> - <?=unix_to_human($row->date_posted, TRUE, 'eu');?></i></p>
<h2>Comments</h2>
<?php foreach($query2->result() as $row2): ?>
<p><b><?=$row2->username;?> - <?=unix_to_human($row2->date_posted, TRUE, 'eu');?></b></p>
<p><?=$row2->comment;?></p>
<?php endforeach; ?>
<?php if (isset($commented) && $commented == TRUE): ?>
<p>Your comment has been submitted.</p>
<?php else: ?>
<?php echo form_open("news/article/$news_id"); ?>
<table border="0" cellpadding="3" cellspacing="1">
<?php echo $this->validation->error_string; ?>
<tr>
<td>Comment</td>
<td><textarea class="textarea" name="comment" cols="30" rows="5"><?=$this->validation->comment;?></textarea></td>
</tr>
</table>
<input type="submit" class="submit" value="Add Comment" />
</form>
<?php endif; ?>