【发布时间】:2014-11-20 20:01:36
【问题描述】:
可能这个问题真的很愚蠢,但我尝试了很多东西,我总是得到同样的错误......
如您所见,我正在尝试在 CodeIgniter 中使用分页,我可以加载包含页面和链接的视图,但是当我单击链接时,它会显示 “找不到对象!”
这是我的控制器:
class Controller extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Blogs_model");
$this->load->library("pagination");
}
public function index() {
$config = array();
$config["base_url"] = base_url() . "pagination/";
$config["total_rows"] = $this->Blogs_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$config['first_link'] = 'First';
$config['last_link'] = 'End';
$config['next_link'] = 'Next →';
$config['prev_link'] = '← Prev';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Blogs_model->fetch_entries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
}
routes.php:
route['default_controller'] = "controller";
$route['pagination/(:any)'] = 'pagination';
$route['404_override'] = '';
config.php:
config['base_url'] = 'http://localhost/ci';
有人可以帮帮我吗?谢谢。
编辑 那是我的模型:
class Blogs_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("entry");
}
public function fetch_entries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("entry");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
问题是,当我加载页面时:localhost/ci/ 它可以正常工作,但是当我单击链接时,url 会更改为 localhost/ci/pagination/2 并且无法正常工作。我想这是路线的问题,但无论如何我不确定。
编辑 2 感谢所有 cmets 人员,最后我设法让它工作,但 Index.php 处于活动状态,我的意思是我的网址现在是:
http://localhost/ci/index.php/controller/index/2
我想在没有 index.php 的情况下让它工作。我现在的代码是这样的: 控制器:
class Controller extends CI_Controller {
public function __construct() {
parent:: __construct();
$this->load->helper("url");
$this->load->model("Blogs_model");
$this->load->library("pagination");
}
public function index() {
$config = array();
$config["base_url"] = "http://localhost/ci/index.php/controller/index";
$config["total_rows"] = $this->Blogs_model->record_count();
$config["per_page"] = 2;
$this->pagination->initialize($config);
$data["results"] = $this->Blogs_model->fetch_entries($config["per_page"], $this->uri->segment(3));
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
}
config.php 中的 base_url 是
http://localhost/ci;
而且路由文件现在只有:
$route['default_controller'] = "controller";
$route['404_override'] = '';
【问题讨论】:
标签: php codeigniter pagination