Welcome Guest, Not a member yet? Register   Sign In
Pagination, offset issue
#1

[eluser]1cookie1[/eluser]
hi
I'm having problems with a pagination script. Ive searched the forums and found a lot of uesful stuff but alas i can't get my script to work. Smile I found this

article came close and I adopted some their code - but still my pagination links don't work correctly.

OK, my code:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Books_model extends Model
{
  function __construct() {
    parent::Model();
  }

  function get_books($num, $offset) {
    $query = $this->db->get('books', $num, $offset);    
    return $query;
  }
  
}


/* End of file books_model.php */
/* Location: ./system/application/models/books_model.php */


Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Books extends Controller
{
    function __construct() {
            
        parent::Controller();
        $this->load->helper('url');
        $this->load->database();
    }


    function index($page = NULL) {
        
        //load pagination class
        $this->load->library('pagination');
        $config['base_url'] = base_url().'index.php/books/index/';
        $config['total_rows'] = $this->db->count_all('books');            //returns 17
        $config['per_page'] = '5';
        $config['full_tag_open'] = '<p>';
        $config['full_tag_close'] = '</p>';
        
        if (isset($page) && is_numeric($page))
            {
              $offset = ($page-1) * $config['per_page'];
            }
            else
            {
              $offset = 0;
            }

        $this->pagination->initialize($config);

        //load the model and get results
        $this->load->model('books_model');
        
        $data['results'] = $this->books_model->get_books($config['per_page'], $offset);
        
        //load the HTML Table class
        $this->load->library('table');
        $this->table->set_heading('Book Id','Auth Id','Title','Company','Description','Isbn');
        
        //load the view
        $this->load->view('books_view',$data);
    }

}


/* End of file books.php */
/* Location: ./system/application/controllers/books.php */


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
&lt;link rel="stylesheet" href='&lt;?php echo base_url(); ?&gt;css/main.css' type="text/css" media="screen, projection" /&gt;
&lt;title&gt;CodeIgniter Pagination Tutorial&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>Book search</h1>
&lt;?php echo $this->table->generate($results); ?&gt;
&lt;?php echo $this->pagination->create_links(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;

There are 17 rows in total in my db, table = 'books'
My test URIs are:

http://localhost/CIgniter/index.php/books/index/1
http://localhost/CIgniter/index.php/books/index/2
http://localhost/CIgniter/index.php/books/index/3
http://localhost/CIgniter/index.php/books/index/4

If I type these manually in my address bar in my browser I get correct results; i.e. the script pulls records:

1 - 5
6 - 10
11 - 15

& lastly records

16 & 17, displaying these to the screen. (please see attached ss)

My links display like:

1 2 3 > Last


Clicking on link 2 displays:

http://localhost/index.php/books/index/5, in my browser address bar and in the page body I get a


404 error
The requested URL /index.php/books/index/5 was not found on this server.


Clicking on link 3 displays:

http://localhost/index.php/books/index/10, followed by

404 error
The requested URL /index.php/books/index/10 was not found on this server. And so on.

Click on Last and get:

http://localhost/index.php/books/index/15

404! Not found.


It's not the uri segment number as I have confirmed that this is correct by manually typing the uri s above.

For a relatively simple concept such as offset and limit, I'm struggling a bit.

Help please. Smile


Messages In This Thread
Pagination, offset issue - by El Forum - 01-12-2010, 04:48 AM
Pagination, offset issue - by El Forum - 01-13-2010, 08:16 AM
Pagination, offset issue - by El Forum - 01-13-2010, 11:15 AM
Pagination, offset issue - by El Forum - 01-13-2010, 04:09 PM
Pagination, offset issue - by El Forum - 01-13-2010, 07:15 PM
Pagination, offset issue - by El Forum - 01-14-2010, 03:09 AM
Pagination, offset issue - by El Forum - 01-14-2010, 04:47 AM
Pagination, offset issue - by El Forum - 01-14-2010, 05:24 AM



Theme © iAndrew 2016 - Forum software by © MyBB