[英]Codeigniter: Pagination, 2 page not found
我一直在努力解決此問題一段時間,但沒有解決。 第二頁顯示404錯誤。 如果有人能抽出時間來檢查代碼,將不勝感激。 我也在該路線上工作,但它仍在2頁上顯示404錯誤。 這是控制器
public function __construct()
{
parent::__construct();
$this->load->model('Products_model');
$this->load->helper('form');
$this->load->library('pagination');
}
public function index($offset = 0) {
//how many blogs will be shown in a page
$limit = 3;
$result = $this->Products_model->get_blogs($limit, $offset);
$data['blog_list'] = $result['rows'];
$data['num_results'] = $result['num_rows'];
// load pagination library
$config = array();
$config['base_url'] = 'http://localhost/nwpgroup/nwp2/index.php';
$config['total_rows'] = $data['num_results'];
$config['per_page'] = $limit;
//which uri segment indicates pagination number
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
//max links on a page will be shown
$config['num_links'] = 5;
//various pagination configuration
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$config['first_tag_open'] = '<span class="first">';
$config['first_tag_close'] = '</span>';
$config['first_link'] = '';
$config['last_tag_open'] = '<span class="last">';
$config['last_tag_close'] = '</span>';
$config['last_link'] = '';
$config['prev_tag_open'] = '<span class="prev">';
$config['prev_tag_close'] = '</span>';
$config['prev_link'] = '';
$config['next_tag_open'] = '<span class="next">';
$config['next_tag_close'] = '</span>';
$config['next_link'] = '';
$config['cur_tag_open'] = '<span class="current">';
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('products', $data);
}
模型部分
public $products = 'products';
public function get_blogs($limit, $offset) {
if ($offset > 0) {
$offset = ($offset - 1) * $limit;
}
$result['rows'] = $this->db->get($this->products, $limit, $offset);
$result['num_rows'] = $this->db->count_all_results($this->products);
return $result;
}
視圖。
<?php
foreach ($blog_list->result() as $blog) {
?>
<div class="post">
<h2 class="title"><?php echo $blog->id; ?></h2>
<p class="meta">
<?php
echo $blog->name;
?>
<div class="entry">
<p><?php echo $blog->price; ?></p>
</div>
</div>
<?php
}
if (strlen($pagination)) {
echo $pagination;
}
?>
感謝您抽出寶貴的時間閱讀此代碼。
您在base_url
中有分頁問題
更改
$config['base_url'] = 'http://localhost/nwpgroup/nwp2/index.php';
至
$config['base_url'] = 'http://localhost/nwpgroup/nwp2/index.php/products/index/';
分頁中的基本url指向從模型中獲取數據的控制器功能。
所以你必須將base_url設置為
$config['base_url'] = site_url("products/index/");
site_url
將使用index.php
返回您的項目URL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.