Welcome Guest, Not a member yet? Register   Sign In
Pagination 404
#1

[eluser]codejack[/eluser]
I'm trying to get pagination working on my blog page but unfortunately it's returning a 404 error.

CONTROLLER:

Code:
class Blog extends CI_Controller

{

function index()

{

  // If user has logged in.
  
  if ($this->session->userdata('username'))
  
  {
  
   $this->load->library('pagination');
   $config['base_url'] = base_url('blog');
   $config['total_rows'] = $this->db->count_all('blog_post');
   $config['per_page'] = '1';
  
   $this->pagination->initialize($config);
  
   $this->load->model('blog_model');
   $data['current_user'] = $this->user_model->current_user();
   $data['posts'] = $this->blog_model->get_all_posts($config['per_page'],$this->uri->segment(2));  
   $this->load->view('blog_view', $data);
  
  }
  
  else
  
  {
  
   // Redirect to the login page.
  
   redirect('login');
  
  }
  
}

}

MODEL:

Code:
class Blog_model extends CI_Model

{

public function get_all_posts($num, $offset)
    
    {
     $this->db->select('blog_post.id, blog_post.user_id, blog_post.title, blog_post.category, blog_post.date_published, blog_post.content, user.username, COUNT(blog_comment.id) AS total_comments', false);
     $this->db->from('blog_post');
  $this->db->join('user', 'user.id = blog_post.user_id');
  $this->db->join('blog_comment', 'blog_comment.post_id = blog_post.id', 'left');
  $this->db->where('blog_post.status_published', 1);
  $this->db->group_by('blog_post.id');
  $this->db->order_by('blog_post.date_published', 'desc');
  $this->db->limit($num, $offset);
  $query = $this->db->get();
    
  return $query->result();
    
    }

}

VIEW (extract):

Code:
<div class="eight columns content">
  
    &lt;?php foreach ($posts AS $post) : ?&gt;
    <div class="box-ribbon">
    
     <div class="ribbon">
    
      <img src="&lt;?php echo site_url('images/icons-blog-category') . '/' . $post-&gt;category . '.png'; ?&gt;" alt="" />
    
      <p>&lt;?php echo clean_date($post->date_published); ?&gt;</p>
      
      <div class="clr"></div>
      
     </div>
    
     <div class="box-ribbon-content">
    
      <h2><a href="#">&lt;?php echo $post->title; ?&gt;</a></h2>
    
      <p><em>by <a href="&lt;?php echo site_url('people') . '/' . $post-&gt;username; ?&gt;">&lt;?php echo $post->username; ?&gt;</a> / &lt;?php echo relative_time($post->date_published) . "ago"; ?&gt; / &lt;?php echo $post->total_comments; ?&gt; comments</em></p>
    
      &lt;?php echo $post->content; ?&gt;
      
     </div> &lt;!-- end box-ribbon-content --&gt;
    
    </div> &lt;!-- end box-ribbon --&gt;
    
    <div class="box-bottom"></div>
    &lt;?php endforeach; ?&gt;
    
    &lt;?php echo $this->pagination->create_links(); ?&gt;
  
   </div> &lt;!-- end content --&gt;

Where am I going wrong? It has the correct number of pagination links etc, but when I click them, it returns a 404.

Thanks in advance.
#2

[eluser]Aken[/eluser]
You need a route, telling CI to direct pagination calls to your blog's index() function. Right now it's trying to find another function inside your Blog class, because that's how CodeIgniter URLs work.

Read through these, and see if you can figure out what route to use (it's pretty simple):
http://ellislab.com/codeigniter/user-gui.../urls.html
http://ellislab.com/codeigniter/user-gui...uting.html
#3

[eluser]codejack[/eluser]
Ok, cheers!

I'll have a read through it all now.
#4

[eluser]InsiteFX[/eluser]
You also need to add this if for custom url's!

The pagination function automatically determines which segment of your URI contains the page number. If you need something different you can specify it.
Code:
// controller/method/page
$config['uri_segment'] = 3;
#5

[eluser]jwright[/eluser]
(I think) this should stop the 404s...

change...
Code:
$config['base_url'] = base_url('blog');

to...
Code:
$config['base_url'] = base_url('blog/index/');


add...
Code:
$config['uri_segment'] = 3;


change...
Code:
$data['posts'] = $this->blog_model->get_all_posts($config['per_page'],$this->uri->segment(2));
to...
Code:
$data['posts'] = $this->blog_model->get_all_posts($config['per_page'],$this->uri->segment(3));
#6

[eluser]Aken[/eluser]
I'd recommend leaving the index part out of the URL, and setting up a route for it instead. The index is an unnecessary addition to your URL structure, and may be slightly detrimental to SEO, user-friendly URLs, etc.

I'd also recommending using site_url() instead of base_url(), as if you ever need to add the index.php back to your URLs for whatever reason, your links won't break.

Code:
// Pagination config
$config['base_url'] = site_url('blog');
$config['uri_segment'] = 2;

// Get posts data
$data['posts'] = $this->blog_model->get_all_posts($config['per_page'], $this->uri->segment(2));

// Route
$route['blog/:num'] = 'blog';
#7

[eluser]jwright[/eluser]
Aken,
Thanks for the route example, I'll be needing that soon enough.

I was setting up paging for an admin page so I didn't care. What I posted was the only way I could get it to work (except I think I did use site_url() myself).
#8

[eluser]agdelacruz02[/eluser]
Hi I have also encountered this 404 problem and I'm still working with it. I would like to ask what does 'blog' means in this part of the code
Code:
$config['base_url'] = site_url('blog');
Is it the name of the controller or something else? tnx..
#9

[eluser]InsiteFX[/eluser]
Code:
$config['base_url'] = 'the directory where CodeIgniter is installed';
#10

[eluser]agdelacruz02[/eluser]
How about in this part
Code:
$route['blog/:num'] = 'blog';
what does 'blog' means here? and also 'num'? tnx.




Theme © iAndrew 2016 - Forum software by © MyBB