Welcome Guest, Not a member yet? Register   Sign In
Show a single blog post?

Hi everyone,

Like a lot of people, I'm learning CI by starting with the video tutorials.

Here is my question.

What do I need to do to go about displaying a single blog post? The video tutorial uses a foreach loop to display all blog entries, I want to include a "Read More" link that allows the user to view a single blog entry. But I have no idea how to do this. I'm pretty sure I need to create a model to get the data, but I've searched around and was not able to find any code showing how to accomplish this.

I have a view in place to handle the single blog post page, now I just need the code to generate the data.

I tried the solution from http://ellislab.com/forums/viewthread/57595/
but I got a ton of errors.

Any help would be great.


First, welcome to CodeIgniter Smile

Before I start, I'd like to know more about your experience with that solution you mentioned. What kind of errors did you get?

Thanks for the welcome and the reply Smile

About the solution....

I tried the code above and received a bunch of notices about the offset regarding the list() line. (I don't have the exact error available right now).

I'm really new to PHP frameworks, so there is a lot of things that are probably obvious that I just don't know about.

Is there a written tutorial, or anywhere I can view a sample application that I could learn from? I'm basically interested in creating a simple blog/CMS type app to learn CI on.

Thanks, any help is greatly appreciated.


I've spent the day trying, and I can't figure this out.

Am I missing somewhere that shows how to get a single result from the database? If so please point me in that direction because I'm not having any luck. I've searched the forums, wiki and user guide, and while I've found information on how you could do it, I don't seem to be able to write the code that actually does it.

If someone could just give me a quick example of a model and controller for displaying a single database record I would be greatly appreciated.

Thanks guys (sorry for being such a noob Sad )

afterspark: Feel free to download InkType (link in my signature) to see how I did it. Hopefully you'll find a lot of common logic in there to learn from (displaying single posts, displaying posts from certain categories, pagination, etc). And of course if you have any questions, just let us know!

Thanks so much, I really appreciate it. Smile

Ok while your blog app is great, its a little over my head.

What I really would love to see, is just a model and controller for displaying a single blog entry.

InkType does a great job of this, but it also has a lot of other things going on, making it hard for me to understand how its working. I really want to understand what's going on with the code, and to do this I think I need a very basic sample.

Thanks guys.

[eluser]Nick Husher[/eluser]
Leaving models out for a second, you could have a controller that looks like this:
class Blog extends Controller {
    function index($entry_id = 0) {
        if($entry_id == 0) {
            // default action is to show the last ten entries

            $this->db->orderby('date_posted DESC');
            $entries = $this->db->get('blog_entries', 10);
            $data['entries'] = $entries;
        } else {
            // otherwise, display a single entry
            $entry = $this->db->getwhere('blog_entries', array('entry_id' => $entry_id));
            if($entry->num_rows() != 0) {
                $data['entry'] = $entry;
            } else {

So going to <your_ci_app>/blog would show you the last ten entries you've posted. Going to <your_ci_app>/blog/5 would take you to the blog entry with the ID of 5.

Nick, thanks for the reply.

I'm using the code you provided to try to display the single record and I have a quick question. Where in the code is the ID at the end of the URL read into the ID of the blog post?

I seem to be having problems associating the ID to the post ID.


[eluser]Nick Husher[/eluser]
CodeIgniter URLs

Really, read the user guide. It's critical to understanding the framework. It's written in Human Language with plenty of examples. It can be difficult holding it all in one's head at a time, but I've found that writing little sandbox tests of new features really helps me wrap my head around (or immerse myself in? which is the better analogy?) the CodeIgniter tao.

Theme © iAndrew 2016 - Forum software by © MyBB