0
votes

Right, using pagination for the first time and CANNOT get it to work beyond the first page (404 error). It all seems to revolve around the $offset variable as if I change the offset's variable to an integer (such as 15), it displays the results on the first page from row 15 onwards - but the $this->uri->segment(2) variable is just not working. It all displays correctly, but second page won't load (as I said, 404 error).

Controller:

<?php
class Brands extends CI_Controller
{
                public function index()
                {
                                $this->load->library('pagination');
                                $config['base_url']   = base_url() . '/brands/';
                                $config['total_rows'] = $this->db->count_all('seixweb');
                                $config['per_page']   = 15;
                                $limit                = $config['per_page'];
                                $offset               = $this->uri->segment(2);
                                $this->pagination->initialize($config);
                                $this->load->model('Default_model');
                                ... Etc.
                }
}

Model:

<?php
class Default_model extends CI_Model
{
                function get_list($limit, $offset)
                {
                                $query = $this->db->get('tbl', $limit, $offset);
                                return $query->result_array();
                }
}

URL (first page):

http://www.mydomain.com/brands

URL (second page):

http://www.mydomain.com/brands/15

What am I doing wrong?

Although it will have nothing to do with the error, view:

        <!-- Products -->
        <div class="products">
            <div class="cl">&nbsp;</div>

            <p><?=$this->pagination->create_links();?></p>

            <div class="cl">&nbsp;</div>
        </div><!-- End Products -->
4
base url should should contains the method name so for your example base_url('brands/index') - ahmad

4 Answers

0
votes

That's because your link is probably incorrect.

The default of codeigniter is www.example.com/controller/action, if you didn't change anything, your link is calling a function named 15 in the controller brands.

You need to set you link as http://www.mydomain.com/brands/yourfunctionname/15 or tell codeigniter you want it look for the second uri segment by $config['uri_segment'] = 2; Not recommended in your case.

0
votes

set the default parameter for OFFSET

$config['per_page'] = $this->uri->segment(3, 15);

the second parameter defines the default value to 15 if $this->uri->segment(3) not present.

0
votes
$config['base_url']   = base_url() . '/brands/';

remove last slash

$config['base_url']   = base_url() . '/brands';

add rule routes.php

$route['brands/(:num)'] = "brands";
0
votes

While fetching the records from the model send your limit and segement value into the model and you will get all the required records