Welcome Guest, Not a member yet? Register   Sign In
YAPI (Yet Another Pagination Issue)
#1

[eluser]cpass78[/eluser]
OK Im stumped...

For some stupid reason i can't get the links to show in my view and i spent more time on it then i like so I'm turning to the community for assistance.

Controller code
Code:
public function listTodos() {
    $this -> load -> library('pagination');  
    $this -> load -> library('table');
    $this -> table -> set_template(array('table_open' => '<table class="zebra-striped">'));
    $this -> data['content'] = 'view_todos';
  
    $config['base_url'] = site_url('my_account/listtodos');
    $config['per_page'] = 5;
    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $todos = $this -> crud_model -> listTados($this -> userid, $this -> orderby, $this -> sort, $config['per_page'], $page);
    echo $config['total_rows'] = count($todos); //prints out 5 correctly out of 9 records
    $this->data['paginate'] = $this -> pagination -> create_links();
    $this -> pagination -> initialize($config);
  
    //$this->data ['category'] = $category;
    $this -> data['todos'] = $todos;
    $this -> load -> view($this -> template . 'backend/template', $this -> data);
}

Model code
Code:
function listTados($userid, $orderby, $sort, $limit, $offset) {
  $query = $this -> db -> where('user_id', $userid) -> order_by($orderby, $sort) -> limit($limit, $offset) -> get('******');
  if ($query -> num_rows > 0) {
   return $query -> result();
  }
  return false;
}

In my view i echo out $paginate, all pretty easy stuff right..

The strange thing is that in profiler my query looks like so
Code:
SELECT *
FROM (`******`)
WHERE `user_id` =  '1'
ORDER BY `id`
LIMIT 5

The offset is not getting passed unless i switch the vars passed to the model but then i only get one record returned and the offset is shown.

Any clues?

Thank you
#2

[eluser]CroNiX[/eluser]
Code:
echo $config['total_rows'] = count($todos); //prints out 5 correctly out of 9 records

total_rows should be the grand total number of rows in your table, not how many results are in the current resultset (which would be only 5 at a time if your per_page is 5). If this number isn't correct it can't calculate the page offsets and number of pages (total_rows / per_page (rounded up) = number of pages). So, that should be 9, not 5. If your per_page was 5 and you had less than 5 total_rows, there would be only 1 page (the current page) and pagination links wouldn't show, which is what's happening.

You need 2 queries. One to get the current results for the current page number (using offset/limit), and the other to get the total rows in the table using the exact same criteria as you use for the first query (all wheres, etc), except no limit/offset.
#3

[eluser]cpass78[/eluser]
see to much time spent... figured it was something stupid i missed. Thank you bud




Theme © iAndrew 2016 - Forum software by © MyBB