probably this problem is really stupid but I tried lot of things and I always get the same error ...
As you can see, I'm trying to use Pagination in CodeIgniter and I can load a view with the pages and links but when I click on a link it says "Object not found!"
This is my Controller:
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';
Could someone help me please? Thank you.
EDIT Thats my model:
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;
}
}
And the problem is that when I load the page: localhost/ci/ it works correctly, but when I click on a link the url changes to localhost/ci/pagination/2 and doesn't work. I guess its a problem of routes but anyway I'm not sure.
EDIT 2 Thanks for all the comments guys, finally I manage to get it working but with the Index.php active, I mean my url now is:
http://localhost/ci/index.php/controller/index/2
and I would like to get it working without the index.php. My code right now is this: Controller:
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);
}
}
And the base_url from the config.php is
http://localhost/ci;
Also the routes file now only has:
$route['default_controller'] = "controller";
$route['404_override'] = '';
Should look a little like this:
Routes File:
$route['blog/(:num)'] = "controller/index/$1";
$route['blog'] = "controller/index";
Controller:
public function index($page = 0) {
$news = $this->blog_model->get_blog($page);
}
Model:
public function get_blog($page) {
$offset = $page * 2;
$stuff = $this->db->get('articles', $offset, 2);
}
Instead of this:
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->Blogs_model->fetch_entries($config["per_page"], $page);
Try this:
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$offset = $page==0? 0: ($page-1)*$config["per_page"];
$data["results"] = $this->Blogs_model->fetch_entries($config["per_page"], $offset);
Unless your model is doing this work you need to pass the offset not the page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.