Codeigniter分页不起作用类别获取页面帖子

时间:2018-12-31 03:18:26

标签: php codeigniter-3

控制器: 类别获取帖子数据。但是如何设置 视图: 查看其工作页面。但是再次单击第二页则无法工作

我尝试了参数-> $ offset = 0

public function posts($id){
    $config['base_url'] = base_url().'posts/category/';
    $config['total_rows'] = $this->db->count_all('posts');
    $config['per_page'] = 4;
    $config['uri_segment'] = 3;
    $config['attributes'] = array('class' => 'pagination-link');
    // Initialize
    $this->pagination->initialize($config);

    $data['title'] = $this->category_model->get_category($id)->name;
    $data['posts'] = $this->post_model->get_posts_by_category($config['per_page'], $id);

    $this->load->view('templates/header');
    $this->load->view('posts/category', $data);
    $this->load->view('templates/footer');
}

3 个答案:

答案 0 :(得分:2)

您说它显示404,这意味着它已被重定向到错误的url,并且无法到达您的控制器,并且完全不依赖于从模型中检索到的数据。

您的base_url也许有问题,因此请确保将其重定向到BASE_URL/posts/category/NUMBER

关于其他分页配置,一切都很好。

答案 1 :(得分:0)

尝试一下:

 $config = array();
 $config["base_url"] = base_url() . "posts/category";
  $config["total_rows"] = $this->post_model->record_count();
   $config["per_page"] = 4;
   $config["uri_segment"] = 3;
   $this->pagination->initialize($config);
  $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
  $data["results"] = $this->post_model->
  get_posts_by_category($config["per_page"], $page);
  $data["links"] = $this->pagination->create_links();
  $this->load->view('templates/header');
    $this->load->view('posts/category', $data);
    $this->load->view('templates/footer');

答案 2 :(得分:0)

也许它可以帮助您, 在控制器中:

$config = array();
$config["base_url"] = base_url() . "index.php/pagination_controller/contact_info";
$total_row = $this->pagination_model->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] = 1;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = '&nbsp;<a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';

$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$data["results"] = $this->pagination_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode('&nbsp;',$str_links );

// View data according to array.
$this->load->view("pagination_view", $data);

在这里用模型替换pagination_model。

现在,在您的视图文件中,只需通过链接将其循环:

<div id="pagination">
<ul class="tsc_pagination">

<!-- Show pagination links -->
<?php foreach ($links as $link) {
echo "<li>". $link."</li>";
} ?>
</ul>
</div>