codeigniter分页下一页404未找到

时间:2016-08-31 09:33:27

标签: php codeigniter

索引页面运行良好,但是当我点击第2页时,它显示404页面未找到。

控制器

public function index() {
   $config = array();
   $config['base_url'] = base_url('admin/booking');
   $total_row = $this->booking_m->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["info"] = $this->booking_m->fetch_data($config["per_page"], $page);
  $str_links = $this->pagination->create_links();
  $data["links"] = explode('&nbsp;',$str_links );


  $this->load->view('admin/booking/index',$data);}

页面网址http://127.0.0.1/admin/booking/1可以正常使用。

http://127.0.0.1/admin/booking/2 //找不到错误

模型

public function fetch_data($limit, $id) {
$this->db->limit($limit);
$this->db->where('id', $id);
$query = $this->db->get("booking");
if ($query->num_rows() > 0) {
    foreach ($query->result() as $row) {
    $data[] = $row;
    }

    return $data;
}
return false;
}

3 个答案:

答案 0 :(得分:2)

如果不尝试

,您是否设置了路线?
$route['admin/booking/(:any)'] = 'controllername/function/$1';

https://www.codeigniter.com/user_guide/general/routing.html?highlight=routes

您的基本网址应该类似于

| http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try guess the protocol and path
| your installation, but due to security concerns the hostname will be set
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!

$config['base_url'] = 'http://localhost/yourproject/';

如果你的网址中有IP地址,那么某些链接可能无效,为什么设置网址并使用localhost而不是IP

另外,请确保按照第https://www.codeigniter.com/user_guide/general/controllers.html#let-s-try-it-hello-world段所述的类和文件名的第一种方式进行操作。{/ 3}}

答案 1 :(得分:1)

您错过了配置。将其放在配置

$config['uri_segment'] = 3;

而不是此配置$config['use_page_numbers'] = TRUE;并尝试

答案 2 :(得分:0)

通过使用inspect-&gt;元素检查第二页重定向到的URL ..