Welcome Guest, Not a member yet? Register   Sign In
Blog system....posts
#1

[eluser]Adam Griffiths[/eluser]
Hey,

I've got a blog system that I am building on top of CI. So far it is really simple, I'm just pulling data out of the database and displaying it in a view. But I am not showing all my posts, just the introduction. How would I make it so I can link to a page, have the URL the same as the title but with '-' instead of spaces (I know there is already a function for this so that won't be a problem) but grab only that post from the database and display it?

So if I have a post called CodeIgniter rocks my socks I can go to www.myurl.com/index.php/post/codeigniter-rocks-my-socks/ and display the post from the databse?

The only part I'm having trouble with is how do I let CI know which post to pull from the database? I had the idea of using the function to make a new string (i.e. codeigniter-rocks-my-socks) for each post then pulling that, but I've no idea how CI would know that the string is in the URL.

I would usually use $_GET but obviously I can't with CI.


Any comments will help greatly.


Thanks,
Adam Griffiths
#2

[eluser]xwero[/eluser]
You have to create an extra field that stores the to url segment transformed title and you select the blog post based on that field.
#3

[eluser]GSV Sleeper Service[/eluser]
I've done exactly that, I save the title using the url_title helper in the database as 'slug'
and my post method looks like this
Code:
function post($slug) {
    $query = $this->db->get_where('posts', array('slug' => $slug));
    if($query->num_rows() > 0) {
        $row = $query->row();
        $this->data['post']['id'] = $row->id;
        $this->data['post']['title'] = $row->title;
        $this->data['post']['body'] = $row->body;
        $this->data['post']['date'] = $row->post_date;
        $this->load->view('blog/post',$this->data);
    } else {
        show_404($slug);
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB