CodeIgniter Forums
Trying to show a single blog post - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Trying to show a single blog post (/showthread.php?tid=30607)

Pages: 1 2 3 4


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]kaos78414[/eluser]
So for practice I'm trying to create a blog with commenting functions and AJAX and all the normal stuff.

The problem I ran into, is I can't seem to pull a single entry for viewing. I have the entries truncated on the first page with a read more link, to bring them into full view.

This is the code for in the controller for viewing a blog entry:
Code:
function view() {
        $data['post'] = $this->blogmodel->get_view();
        $data['content'] = 'postview';
        
        $this->load->view('includes/template', $data);
    }

And this is in the model:
Code:
function get_view(){
        $this->db->where('blogId', $this->uri->segment(3));
        $query = $this->db->get('blogposts');
        
        if ($query->num_rows() == 1) {
            return $query->row();
        }
        
        return FALSE;
    }

And the view has:
Code:
<div id="post">
    <div id="post-title">
        &lt;?php echo $post->blogTitle; ?&gt;
    </div>
    <div id="post-body">
        &lt;?php echo $post->blogBody; ?&gt;
    </div>
</div>


I've done some tutorials, but I just can't seem to see what I'm doing wrong here. Can anyone enlighten me?


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]kaos78414[/eluser]
Btw, the error I get says that the problem is in the view:


A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/postview.php

Line Number: 3

And also line number 6, same error. Any help is appreciated


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]kaos78414[/eluser]
I found part of the problem, the URI segment isn't getting passed correctly for some reason. If I change $this->uri->segment(3) to $this->uri->segment(3, 1), it passes 1 and the blog with the id 1 shows up correctly.

So the uri segment is passing FALSE to the view, which is a non-object. I'm not sure why this is happening.


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]kaos78414[/eluser]
Further investigation:

It's not my .htaccess file.

There are no syntax errors. The url segment simply isn't getting passed to the model. I can't figure out why. Does anyone have any idea why this is happening? I've autoloaded the correct libraries and helpers. I don't see what could be causing this issue.

EDIT: Changing the code around, i moved the segment call to the function in the controller, passing it as a variable to the function in the view. This produces the same result. Any ideas?


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]Mr. Pickle[/eluser]
Try approaching it as an array. The error states the $post var is not an object.


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]Mr. Pickle[/eluser]
You can also put
Code:
print_r($post);
in your view to see what's in the var and what type of var it is.\
If it says false, you now there is nowt data in it.


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]kaos78414[/eluser]
How exactly do I do that? The $post var should be returning $this->uri->segment(3). How do I list that as an array?

print_r($post); shows nothing at all.


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]danmontgomery[/eluser]
Make sure you're not loading the url library twice... If you load it more than once, the url information is lost.


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]kaos78414[/eluser]
Nope everything looks fine. This is driving me crazy haha


Trying to show a single blog post - El Forum - 05-20-2010

[eluser]cahva[/eluser]
How about if you change your code like this:

Controller:
Code:
function view($blog_id = FALSE) {
    if (!$blog_id)
    {
        return FALSE;
    }
    
    $data['post'] = $this->blogmodel->get_view($blog_id);
    
    if (!$data['post'])
    {
        // Maybe check the $data['post'] here and load a error view for non existing blogid
        $this->load->view('error');
        return FALSE;
    }
    
    $data['content'] = 'postview';

    $this->load->view('includes/template', $data);
}

and the model:
Code:
function get_view($blog_id){
    $this->db->where('blogId', $blog_id);
    $query = $this->db->get('blogposts');
    
    if ($query->num_rows() == 1) {
        return $query->row();
    }
    
    return FALSE;
}

As you see I removed using of $this->uri->segment(3). You can get the blogid just by adding it as a parameter to view method.