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

[eluser]Skoobi[/eluser]
Hi im having slight issues trying to get the pagination to work. I think its because im using routes...

Ive tried a few thing but i cant seem to get it to work... It doesn't seem to choose 5 posts it displays all of them and then if you click the next page it throws a 404.

Heres the controller:

Code:
//-------------------------------------------------------------------------------
// PAGES
//--------------------------------------------------------------------------

public function index()
{
  $this->load->library('pagination');

  $data['pageTitle'] = 'My Title';
  $data['posts'] = $this->posts_model->get_posts();
  $postCount = $this->posts_model->get_post_count();

  $this->load->library('pagination');
  
  $config['base_url'] = 'http://www.my_url.com/blog/';
  $config['total_rows'] = $postCount;
  $config['per_page'] = 5;
  $config['uri_segment'] = 2;

  
  $config['full_tag_open'] = '<ul class="pagination">';
  $config['full_tag_close'] = '</ul>&lt;!--pagination--&gt;';
  
  $config['first_link'] = '&laquo; First';
  $config['first_tag_open'] = '<li class="prev page">';
  $config['first_tag_close'] = '</li>';
  
  $config['last_link'] = 'Last &raquo;';
  $config['last_tag_open'] = '<li class="next page">';
  $config['last_tag_close'] = '</li>';
  
  $config['next_link'] = 'Next &rarr;';
  $config['next_tag_open'] = '<li class="next page">';
  $config['next_tag_close'] = '</li>';
  
  $config['prev_link'] = '&larr; Previous';
  $config['prev_tag_open'] = '<li class="prev page">';
  $config['prev_tag_close'] = '</li>';
  
  $config['cur_tag_open'] = '<li class="active"><a href="">';
  $config['cur_tag_close'] = '</a></li>';  

  $config['num_tag_open'] = '<li class="page">';
  $config['num_tag_close'] = '</li>';

  // $config['anchor_class'] = 'follow_link';

  $this->pagination->initialize($config);

  $data['pagination']  = $this->pagination->create_links();

  $this->load->view('includes/header', $data);
  $this->load->view('posts/index', $data);
  $this->load->view('includes/footer');
}

Heres the model:

Code:
public function get_posts()
{
  $this->db->order_by('dateAdded', 'desc');
  $query = $this->db->get('posts');
  return $query->result_array();
}

public function get_single_post($title)
{
  $query = $this->db->get_where('posts', array('niceTitle' => $title));
  return $query->row_array();
}

  public function get_post_count()
{
  $query = $this->db->select()->where('status', 1)->limit(5)->count_all_results('posts');
  return $query;
}

and the routes:

Code:
$route['blog'] = "posts";
$route['blog/(:num)'] = "posts/$1";
$route['blog/(:any)'] = "posts/post/$1";


Any Ideas???

Cheers
Chris
#2

[eluser]Tpojka[/eluser]
You don't need to limit counting rows in model.
You need to use same query as in get_posts() function with ending num_rows() in your model so you will get correct $postCount value. What is written down in link hover status bar when you move mouse onto next button?
#3

[eluser]Skoobi[/eluser]
Hi cheers for your reply.

Is this what you mean with the getposts?

Model:
Code:
public function get_post_count()
{
  $query = $this->db->get('posts');
  return $query->num_rows();
}

the hover link is http://my_site.com/blog/5
#4

[eluser]CroNiX[/eluser]
I think one problem is here:
Code:
$route['blog/(:num)'] = "posts/$1";

So if you had a request for www.yoursite.com/blog/4, it would send it to "posts" controller and a method named "4", which obviously can't exist. So try adding the actual method in there like you did here:
Code:
$route['blog/(:any)'] = "posts/post/$1";
#5

[eluser]Tpojka[/eluser]
First, in routes.php file you need:
Code:
$route['blog'] = "posts/index";
$route['blog/(:num)'] = "posts/index/$1";
Secondly, you need to pass an argument in index method false by default.
That argument should be used for counting offset.
Check this video (it is 1.x.x. version of CI so there will be some code distinction). P.S. start at 3:00 minute.
#6

[eluser]Skoobi[/eluser]
Guys!! Your absolute legends. This is why i love the codeigniter framework, people help!
Many thanks




Theme © iAndrew 2016 - Forum software by © MyBB