• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Route and Pagination

#1
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 
Code:
$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)
                                                 ->order_by('
blog_date', 'DESC')
                                                 ->get('
blogs', $data['per_page'], $this->uri->segment(3));
        $data['
full_tag_open'] = '<ul class="pagination pagination-sm">';
        $data['
full_tag_close'] = '</ul>';
        $data['
first_link'] = false;
        $data['
last_link'] = false;
        $data['
first_tag_open'] = '<li>';
        $data['
first_tag_close'] = '</li>';
        $data['
prev_link'] = false;
        $data['
prev_tag_open'] = '<li class="prev">';
        $data['
prev_tag_close'] = '</li>';
        $data['
next_link'] = false;
        $data['
next_tag_open'] = '<li>';
        $data['
next_tag_close'] = '</li>';
        $data['
last_tag_open'] = '<li>';
        $data['
last_tag_close'] = '</li>';
        $data['
cur_tag_open'] = '<li class="active"><a href="#">';
        $data['
cur_tag_close'] = '</a></li>';
        $data['
num_tag_open'] = '<li>';
        $data['
num_tag_close'] = '</li>';
        $this->pagination->initialize($data);
        $data['
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: 
Code:
$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.
Headpetrol
Reply

#2
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",
];
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 0;
}
$this->pagination->initialize($config);
$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);
$this->db->order_by("id","desc");
$query = $this->db->get($tabel_name);
return $query->result_array();
}

I hope this will help you Smile
Reply

#3
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
    
}

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.