0

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'] = '';

2 Answers 2

0

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);
}
1
  • This is an example, make sure if you have index.php still enabled you set it up accordingly. Sep 26, 2014 at 8:28
0

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.

6
  • question: are you using index.php in your url and what does your htaccess look like if you have dont have index.php in the url can you give an example url you are using for the page both first load and page 2 or 3?
    – henry
    Sep 26, 2014 at 7:17
  • I don't use index.php in my url. This is the content of .htaccess: RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] SOrry don't know how to format this here :S
    – Slei
    Sep 26, 2014 at 14:15
  • you didnt give the urls i was asking about: please give the page url on first load and give me the page url for what pagination is outputting for page 2 or 3
    – henry
    Sep 26, 2014 at 16:50
  • can you update your answer/or comment for the code you did in regards to James's suggestion? you just said "not working"...maybe you made a mistake...might be helpful to show the code you added or change? the error you are getting seems like more of a route problem...although I still think my answer is another issue.
    – henry
    Sep 26, 2014 at 16:57
  • Right now I'm not at home so I can't update with the code I tried, but the URLS are those: localhost/ci/ when first load (this works and shows the list) and when I click on a link thats the url: localhost/ci/pagination/2. This is with my code, can't say right now with the James suggestion, sorry. I'm completely sure it must be a routes problem. I started using CodeIgniter less than 3 days and I've had lot of problems with routes during this time with CodeIgniter :S
    – Slei
    Sep 26, 2014 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.