Jefri Fernando Jefri Fernando - 1 month ago 10
PHP Question

How to make pagination with category post Codeigniter

anybody know how to fix my problem..
my problem is for pagination based on category work but when i click button pagination the result is page not found maybe there is something wrong with my routes but i'm not sure about that .. please help me..
this my route

$route['blog/category/:any'] = 'Blog_Category_Post/page';


this my controller

public function page($offset=0)
{
$id=$this->uri->segment('3');
$this->db->select('id,categoryId');
$this->db->from('t_blog_post');
$this->db->where('categoryId',$id);
$query= $this->db->get();

//$jml = $this->db->get('t_blog_post');
$config['base_url'] = base_url().'blog/category/'.$id.'/page';
$config['total_rows'] = $query->num_rows();
$config['per_page'] = 2; /*Jumlah data yang dipanggil perhalaman*/
$config['uri_segment'] = 4; /*data selanjutnya di parse diurisegmen 3*/
/*Class bootstrap pagination yang digunakan*/
$config['full_tag_open'] = "<ul class='pagination pagination-sm' style='position:relative; top:-25px;'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);

$list_blogPost=$this->BlogCategory->get_post($config['per_page'], $offset,$id);
$list_lastblogPost=$this->BlogCategory->get_last_post();
$list_blogCategory=$this->BlogCategory->get_category();
$data=array(
'title' => 'Blog',
'userId' => $this->session->userdata('sh_userId'),
'fullName' => $this->session->userdata('sh_fullName'),
'accountTypeId' => $this->session->userdata('sh_accountTypeId'),
'photo' => $this->session->userdata('sh_photo'),
'halaman' => $this->pagination->create_links(),
'offset' => $offset,
'listblogPost' => $list_blogPost,
'listlastblogPost'=> $list_lastblogPost,
'listblogCategory'=>$list_blogCategory
);
$this->template->display_admin('blog_categorypost',$data);
}

Answer

As your URL has two params you need to set TWO routes for the method. Change your route as

$route['blog/category/(:any)/page'] = 'Blog_Category_Post/page/$1/page';
$route['blog/category/(:any)/page/(:any)'] = 'Blog_Category_Post/page/$1/page/$2';

and now change config line base_url as following

$config['base_url']  = site_url('blog/category').'/'.$id.'/page';

finally your page controller should be

    public function page($categoryId){
     $this->db->select('id,categoryId');
     $this->db->from('t_blog_post');
     $this->db->where('categoryId',$categoryId);
     $query= $this->db->get();

    //$jml                        = $this->db->get('t_blog_post');
     $config['base_url']         = site_url('blog/category/').'/'.$categoryId.'/page';
     $config['total_rows']       = $query->num_rows();
     $config['per_page']         = 2; /*Jumlah data yang dipanggil perhalaman*/  
    $config['uri_segment']      = 5;    /*data selanjutnya di parse diurisegmen 3*/
    /*Class bootstrap pagination yang digunakan*/
     $config['full_tag_open']    = "<ul class='pagination pagination-sm' style='position:relative; top:-25px;'>";
     $config['full_tag_close']   ="</ul>";
     $config['num_tag_open']     = '<li>';
     $config['num_tag_close']    = '</li>';
     $config['cur_tag_open']     = "<li class='disabled'><li class='active'><a href='#'>";
     $config['cur_tag_close']    = "<span class='sr-only'></span></a></li>";
     $config['next_tag_open']    = "<li>";
     $config['next_tagl_close']  = "</li>";
     $config['prev_tag_open']    = "<li>";
     $config['prev_tagl_close']  = "</li>";
     $config['first_tag_open']   = "<li>";
     $config['first_tagl_close'] = "</li>";
     $config['last_tag_open']    = "<li>";
     $config['last_tagl_close']  = "</li>";
     $this->pagination->initialize($config);

    if ($this->uri->segment(5)) {
            $offset = $this->uri->segment(5);
        } else {
            $offset = 0;
        }

     $list_blogPost=$this->BlogCategory->get_post($config['per_page'], $offset,$categoryId);
     $list_lastblogPost=$this->BlogCategory->get_last_post();
     $list_blogCategory=$this->BlogCategory->get_category();
     $data=array(
        'title'          => 'Blog',
        'userId'         => $this->session->userdata('sh_userId'),
        'fullName'       => $this->session->userdata('sh_fullName'),
        'accountTypeId'  => $this->session->userdata('sh_accountTypeId'),
        'photo'          => $this->session->userdata('sh_photo'),
        'halaman'        => $this->pagination->create_links(),
        'offset'         => $offset,
        'listblogPost'   => $list_blogPost,
        'listlastblogPost'=> $list_lastblogPost,
        'listblogCategory'=>$list_blogCategory
        );
     $this->template->display_admin('blog_categorypost',$data);
}

Now test.