Welcome Guest, Not a member yet? Register   Sign In
Building a Blog with Codeigniter tutorial question [Solved!]
#1

[eluser]Mantra of Doom[/eluser]
Hello everyone,
I've been following a tutorial called Building a Blog with Codeigniter on eeinsider.com, which is a pretty good tutorial for me with the exception that it promises a fourth part that was never posted.

I noticed that in the link to the post with the full post, it uses the following code to make the link:
Code:
a href="/blog/entry/&lt;?php echo $entry->url_title; ?&gt;">&lt;?php echo $entry->title; ?&gt;</a>

It seems that this method only really works if there is an .htaccess file to take out the "index.php" from the url. This seems to work okay, but I wonder if there isn't a better way.

I'm just wondering if this might be the reason I'm having issues adding pagination to a blog built with this tutorial. Any hints or a poke in the right direction would be helpful. I love codeigniter so far and the user guide and community are full of helpful things. Thanks in advance for any advice.

I'm posting the code for those that have not seen this tutorial yet.

The Model
Code:
//Gets all entries in the blog table
    public function get_all_entries()
    {
        $this->db->select('summary, title, url_title, author');
        $query = $this->db->get('blog');
        
        if ($query->num_rows() > 0)
        {
          return $query->result();
        } else {
          return array();
        }

    }
    
    //gets a single entry based on its url title
    public function get_entry($url_title)
    {
      $this->db->select('title, entry, author')->where('url_title', $url_title);
      $query = $this->db->get('blog', 1);
      
      if ($query->num_rows() == 1)
      {
        return $query->row();
      } else {
        return false;
      }
    }
The View
Code:
<h2>Blog</h2>
  &lt;?php foreach ($query as $entry) { ?&gt;
  <div class="blog_entry">
    <h2>< a href="/blog/entry/&lt;?php echo $entry->url_title; ?&gt;">&lt;?php echo $entry->title; ?&gt;</a></h2>
    <span class="metadata">&lt;?php echo $entry->author; ?&gt;</span>
    <p>&lt;?php echo $entry->summary; ?&gt;</p>
  </div>
  &lt;?php } ?&gt;
#2

[eluser]bretticus[/eluser]
Check out the anchor function of the URL Helper.

Also, for more up-to-date tutorials, check out the nettuts CodeIgniter from Scratch video series.
#3

[eluser]Cesar Kohl[/eluser]
I would do something like this:

First, enable URL Helper:

Code:
$this->load->helper('url');

Second, use anchor function to create a link:

Code:
anchor('blog/entry'.$entry->url_title,$entry_title)
#4

[eluser]Mantra of Doom[/eluser]
Thanks bretticus and Cesar Kohl, the URL helper is what I was looking for.

The only thing with this method
Code:
anchor('blog/entry'.$entry->url_title,$entry_title)

is that I get this error:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: entry_title

Filename: news/news_main.php

Line Number: 12

Line 12 is where my anchor tag is. Where is it that I define $entry_title since $entry comes from the query loop in the view?
#5

[eluser]LuckyFella73[/eluser]
Try:
Code:
anchor('blog/entry'.$entry->url_title,$entry->title)

instead of:
Code:
anchor('blog/entry'.$entry->url_title,$entry_title)
#6

[eluser]Mantra of Doom[/eluser]
I feel really stupid now, thanks LuckyFella73!

I thought I had tried that and it didn't work. I guess I must have missed something simple.

Thanks for all the help guys, I'm going to mark this as solved.
#7

[eluser]LuckyFella73[/eluser]
Naaah don't feel stupid - making such kind of mistakes will
never end. They just become rare more and more Wink




Theme © iAndrew 2016 - Forum software by © MyBB