CodeIgniter Forums

Full Version: Route and Pagination
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone!

I need your help with routes and pagination. I am creating a tags page and can't figure out how to
setup my route and pagination to work properly.

Here is my route 
$route['tag/(:any)/(:num) = 'tag/page/$1';

 Here is my pagination code 
PHP Code:
$id $this->model->get_tag_id($this->uri-segment(2);

$data['base_url'] = base_url().'/tag/'.$this->uri-segment(2);
       $data['total_rows'] = $this->db->select('*')->get('blogs')->num_rows();
       $data['per_page'] = 10;
       $data['num_links'] = 3;
       $data['blogs_with_same_tag''] = $this->db->select('*')->where_in('blog_id', $id)
blog_date', 'DESC')
blogs', $data['per_page'], $this->uri->segment(3));
full_tag_open'] = '<ul class="pagination pagination-sm">';
full_tag_close'] = '</ul>';
first_link'] = false;
last_link'] = false;
first_tag_open'] = '<li>';
first_tag_close'] = '</li>';
prev_link'] = false;
prev_tag_open'] = '<li class="prev">';
prev_tag_close'] = '</li>';
next_link'] = false;
next_tag_open'] = '<li>';
next_tag_close'] = '</li>';
last_tag_open'] = '<li>';
last_tag_close'] = '</li>';
cur_tag_open'] = '<li class="active"><a href="#">';
cur_tag_close'] = '</a></li>';
num_tag_open'] = '<li>';
num_tag_close'] = '</li>';
pagination'] = $this->pagination->create_links(); 

The final question I have is how to use $data['per_page'] and $this->uri->segment(number) if you I create an own query with 
UNION ALL like this: 
$this->db->query("SELECT total.* FROM (
        (SELECT blogpost_id,blog_title, blog_flash, blog_date, blog_status
                WHERE blog_status = '1' AND blogpost_id IN($id))
        UNION ALL
        (SELECT  podcastpost_id,pcast_title, pcast_flash, pcast_date, pcast_status FROM  pcasts
                 WHERE pcast_status = '1' AND pcast_id IN ($id))
                 ) as total")->result_array();

Hopefully someone can help me with these two problems. 
Best regards.
No need to define a route for particular pagination
define code below for pagination controller

============= Controller ================

$config = [
'base_url' => base_url('controlor_name/function'),
'per_page' =>"number of per page records",
'total_rows' => "total_records",
$page = ($this->uri->segment(3)) ;
$page = 0;
$this->pagination_model->fetch_data('tablename',$per_page, $page );

============ Function for Model ============

public function fetch_data($tabel_name , $limit, $id ) {
$this->db->limit($limit, $id);
$query = $this->db->get($tabel_name);
return $query->result_array();

I hope this will help you Smile
In your example, is tag the controller and page method?

In that case you need to add one more argument to router code - $2 to get page number
PHP Code:
$route['tag/(:any)/(:num) = 'tag/page/$1/$2'; 

And controller class:
PHP Code:
class Tag extends CI_controller
    public function 
page($tag false$page 1)
// ... your code here