PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

[FIXED] I'm having a problem with codeigniter pagination

 March 05, 2022     arrays, codeigniter, codeigniter-3, php, variables     No comments   

Issue

When I create pagination without category it works, but with the category, it works with wrong data. With category pagination, it should show the data of a single category. But unfortunately, it shows mixed category data. These things work fine on the Index function..... In my index controller...

    public function index(){
    
    $this->db->where('post_active',1);
    $rows = $this->db->count_all_results('blogs');
    
    // Pagination Config    
    $config['base_url'] = base_url().'blog';
    $config['total_rows'] = $rows;
    $config['per_page'] = 3;
    $config['uri_segment'] = 2;
    $config['use_page_numbers'] = TRUE;
    $config['first_link'] = '<i class="fa fa-angle-double-left"></i>';
    $config['last_link'] = '<i class="fa fa-angle-double-right"></i>';
    $config['prev_link'] = '<i class="fa fa-angle-left"></i>';
    $config['next_link'] = '<i class="fa fa-angle-right"></i>';
    $config['attributes'] = array('class' => 'pagination-link');
    $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
    $pageLimit = ($page*$config['per_page']);
    // Init Pagination
    $this->pagination->initialize($config);
    //meta title, discription, keywords
    $data['meta_title'] = 'Your meta title';
    $data['meta_description'] = 'Your meta description';
    $data['meta_keywords'] = 'Your meta keywords';
    //data
    $data['title'] = 'Latest Posts';
    $data['blogs'] = $this->Blog_model->get_posts(FALSE, $config['per_page'], $pageLimit);
    $data['categories'] = $this->Blog_model->get_categories();
    //view
    $data['main_content'] = 'blog';
    $this->load->view('include/template',$data);
}

and my model for the index is ...

    public function get_posts($slug = FALSE, $limit = TRUE, $offset = FALSE){
        if($limit){
            $this->db->limit($limit, $offset);
        }
        if($slug === FALSE){
            $this->db->order_by('blogs.post_id', 'DESC');
            $this->db->join('category', 'category.category_name = blogs.category');
            $query = $this->db->get('blogs');
            return $query->result_array();
        }
    
        $query = $this->db->get_where('blogs', array('post_slug' => $slug));
        return $query->row_array();
    }

It works perfectly. But when I try this on another function (category) It shows the data count right but shows wrong data in other pagination. Mainly this function is for categories the post data. Here is my controller...

    public function category($category_name){
    
    $rows = $this->db->where(['category'=>$category_name])->from("blogs")->count_all_results();
    // Pagination Config
    $config['base_url'] = base_url().'blog/category/'.$category_name;
    $config['total_rows'] = $rows;
    $config['per_page'] = 3;
    $config['uri_segment'] = 4;
    $config['use_page_numbers'] = TRUE;
    $config['first_link'] = '<i class="fa fa-angle-double-left"></i>';
    $config['last_link'] = '<i class="fa fa-angle-double-right"></i>';
    $config['prev_link'] = '<i class="fa fa-angle-left"></i>';
    $config['next_link'] = '<i class="fa fa-angle-right"></i>';
    $config['attributes'] = array('class' => 'pagination-link');
    $page = ($this->uri->segment(4)) ? ($this->uri->segment(4)-1) : 0;
    $pageLimit = ($page*$config['per_page']);
    // Init Pagination
    $this->pagination->initialize($config);
    
    //meta title, discription, keywords
    $data['meta_title'] = 'Your meta title';
    $data['meta_description'] = 'Your meta description';
    $data['meta_keywords'] = 'Your meta keywords';
    //data
    
    $data['cat'] = $this->Blog_model->get_categories();
    $data['title'] = $category_name;
    $data['categories'] = $this->Blog_model->get_posts_by_category($category_name, FALSE, $config['per_page'], $pageLimit);
    //view
    $data['main_content'] = 'blog_category';
    $this->load->view('include/template',$data);
}

And the model for this controller is ...

    public function get_posts_by_category($category_name, $slug = FALSE, $limit = TRUE, $offset = FALSE){
        if($limit){
            $this->db->limit($limit, $offset);
        }
        if($slug === FALSE){
            $this->db->order_by('blogs.post_id', 'DESC');
            $this->db->join('category', 'category.category_name = blogs.category');
            $query = $this->db->get_where('blogs');
            return $query->result_array();
        }
    
        $query = $this->db->get_where('blogs', array('category_name' => $category_name,'post_slug' => $slug));
        return $query->row_array();
    }

Please help me solve this problem.


Solution

In your function get_posts_by_category()

if($slug === FALSE){
            $this->db->order_by('blogs.post_id', 'DESC');
            $this->db->join('category', 'category.category_name = blogs.category');
            $query = $this->db->get_where('blogs');
            return $query->result_array();
        }

You didn't filter the posts of current category, so it returns mixed categories data. You can update like this:

$query = $this->db->get_where('blogs',  array('category_name' => $category_name));


Answered By - Canh
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing