我在使用 codeigniter 分页时遇到问题
I'm having a problem with codeigniter pagination
当我在没有类别的情况下创建分页时,它可以正常工作,但在有类别的情况下,它会处理错误的数据。使用类别分页,它应该显示单个类别的数据。但不幸的是,它显示了混合类别数据。
这些东西在 Index 函数上运行良好......
在我的索引控制器中...
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);
}
我的索引模型是...
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();
}
效果很好。
但是当我在另一个函数(类别)上尝试这个时,它显示了正确的数据计数,但在其他分页中显示了错误的数据。此功能主要用于分类 post 数据。这是我的控制器...
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);
}
这个控制器的型号是...
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();
}
请帮我解决这个问题。
在你的函数中 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();
}
您没有过滤当前类别的帖子,所以returns混合了类别数据。您可以这样更新:
$query = $this->db->get_where('blogs', array('category_name' => $category_name));
当我在没有类别的情况下创建分页时,它可以正常工作,但在有类别的情况下,它会处理错误的数据。使用类别分页,它应该显示单个类别的数据。但不幸的是,它显示了混合类别数据。 这些东西在 Index 函数上运行良好...... 在我的索引控制器中...
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);
}
我的索引模型是...
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();
}
效果很好。 但是当我在另一个函数(类别)上尝试这个时,它显示了正确的数据计数,但在其他分页中显示了错误的数据。此功能主要用于分类 post 数据。这是我的控制器...
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);
}
这个控制器的型号是...
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();
}
请帮我解决这个问题。
在你的函数中 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();
}
您没有过滤当前类别的帖子,所以returns混合了类别数据。您可以这样更新:
$query = $this->db->get_where('blogs', array('category_name' => $category_name));