Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function on a non-object in
#1

[eluser]karloff[/eluser]
hi guys, probably a simple answer here

I have created a simple blog which worked fine until I tried to include the blog post with my comments any ideas?
the error

Quote:Fatal error: Call to a member function on a non-object in c:\instantrails-2.0-win\www\system\application\views\comment_view.php on line 8

here is my code

application/blog.php
Code:
<?php

class Blog extends Controller {
    function blog ()
    {
        parent::Controller();
        
        $this->load->scaffolding('entries');
        $this->load->helper('form');
        $this->load->helper('url');
    }
    function index ()
    {
        $data['title'] = "Blog";
        $data['heading'] = "Blog";
        $data['query'] = $this->db->get('entries');
        $this->load->view('blog_view', $data);
        
    }
    function posts ()
    {
        }
    function comments ()
    {

        $this->db->where('id', $this->uri->segment(3));
        $query = $this->db->get('entries');
        $data = $query->result();
        // This shold have been a JOIN
        $this->db->where('entry_id', $this->uri->segment(3));
        $query2 = $this->db->get('comments');
        $data2 = $query2->result();
        
      

        $this->load->view('comment_view', array('data' => $data, 'data2' => $data2));
    }
function comments_insert ()
    {
        $this->db->insert('comments', $_POST);
        
        redirect('blog/comments/'.$_POST['entry_id']);
    }

}

?>

views/comment_view.php
Code:
<?=$this->load->view('header')?>  
<?php foreach ($data as $row): ?>
    <h3>&lt;?=$row->title ?&gt;</h3>

    <p>&lt;?=$row->body ?&gt;</p>
&lt;?php endforeach ?&gt;

&lt;?php if ($query2->num_rows() > 0): ?&gt;
    &lt;?php foreach($query->result() as $row2): ?&gt;
    <div class="block">
    <h3>&lt;?=$row2->body?&gt;</h3>
    <p>&lt;?=$row2->author?&gt;</p>
    
    </div>    
    &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
<div class="block">
<p>&lt;?=anchor('blog', 'back to blog');?&gt;</p>

&lt;?=form_open('blog/comments_insert');?&gt;

&lt;?=form_hidden('entry_id', $this->uri->segment(3));?&gt;

<p>&lt;textarea name="body" rows="10"&gt;&lt;/textarea&gt;</p>
<p>&lt;input type="text" name="author" /&gt;</p>
<p>&lt;input type="submit" value="Submit Comment" ?&gt;</p&gt;

&lt;/form&gt;
</div>
&lt;?=$this->load->view('footer')?&gt;
#2

[eluser]Seppo[/eluser]
Well... $query2 is not set, indeed... try count($data) > 0 instead of $query2->num_rows() > 0
#3

[eluser]karloff[/eluser]
hmm, i though that when i called this in my blog.php it called it

$query2 = $this->db->get('comments');
$data2 = $query2->result();

obviously wrong,

I tried what you said but still getting the same error
#4

[eluser]Armchair Samurai[/eluser]
You're only passing the variables $data and $data2 to the view. Try adding them to your second parameter or the vars function if you need them (although you could probably refactor your code to clean things up a bit).
Code:
/**
| In your controller
*/
$this->load->vars(array(
    'data' => $data,
    'data2' => $data2,
    'query' => $query,
    'query2' => $query2
));
$this->load->view('comment_view');
#5

[eluser]karloff[/eluser]
right, think I've got it. thanks.

still much to learn.




Theme © iAndrew 2016 - Forum software by © MyBB