Welcome Guest, Not a member yet? Register   Sign In
pagination and uri_segment
#1

[eluser]fanch[/eluser]
Hello!

i'm too new to CI to prentend having ancounter a bug. There's must be a mistake in my code! I've read documentation and browsed forums but i can't find where!

Any help is welcome:

I want to a simple pagination with sorting features. Here is the code:
Code:
<?php
class Projects extends CI_controller {

function display($sort_by = 'id', $sort_order='desc', $offset = 0) {

  $limits = 30;

  $this->load->model('project_model');

  $results = $this->project_model->search($limits,$offset,$sort_by, $sort_order);
  $data['projects'] = $results['rows'];
  $data['num_results'] = $results['num_rows'];

  //pagination
  $this->load->library('pagination');
  $config = array();
  $config['base_url']=site_url("projects/display/$sort_by/$sort_order/$offset");
  $config['total_rows'] = $data['num_results'];
  $config['per_page'] = $limits;
  $config['uri_segment'] = 5;

  $this->pagination->initialize($config);
  $data['pagination']= $this->pagination->create_links();

  $this->load->view('projects',$data);

        }

}
?>

The search function works great, i can type url such like:
Quote:http://10.153.9.6/dev/index.php/projects...ay/id/desc
http://10.153.9.6/dev/index.php/projects...id/desc/10
http://10.153.9.6/dev/index.php/projects...ty/asc/400
http://10.153.9.6/dev/index.php/projects...ame/asc/11

But the pagination links are wrong, I obtain:
Quote:http://10.153.9.6/dev/index.php/projects...ay/id/desc/0/240
instead of:
Quote:http://10.153.9.6/dev/index.php/projects...ay/id/desc/240
I thought the problem was in the "uri_segment', but I think 5 is the correct value:
Code:
// base url                    / 1      / 2     / 3/ 4  / 5
http://10.153.9.6/dev/index.php/projects/display/id/desc/240

I've missed something. Any idea?

thanks!

fanch
#2

[eluser]182armin[/eluser]
Hello,

I am too new to CI,
but i think its beacouse of your offset in function display:
Code:
function display($sort_by = 'id', $sort_order='desc', $offset = 0)
#3

[eluser]InsiteFX[/eluser]
For help you need to show the code for:
Code:
$this->project_model->search($limits,$offset,$sort_by, $sort_order);
#4

[eluser]bosse2nage[/eluser]
Thanks Armin but I have to passed $offset in my function display. Haven't I?

InsiteFx: sure here is the code of the search function:

Code:
function search($limits, $offset, $sort_by, $sort_order) {

$q = 'SELECT z2.* FROM (';
$q = $q.'SELECT ROW_NUMBER() OVER(ORDER BY '.$sort_by.' '.$sort_order.') AS rownum, z1.*';
$q = $q.'FROM ( SELECT id, code, name, status, keywords, country, company, last_modification FROM _projects) z1';
$q = $q.') z2 WHERE z2.rownum BETWEEN '.($offset+1).' AND '.($offset+$limits).' ;';

$q = $this->db->query($q);
$ret['rows'] = $q->result();

// count query
$q = $this->db->query('SELECT COUNT(*) as count FROM _projects');

$tmp = $q->result();

$ret['num_rows'] = $tmp[0]->count;


return $ret;
}

This function works well: when I enter the search criteria "by hand" in the url, it displays the correct result.

Note that the SQL query is adapted to Sql-Server.

#5

[eluser]bosse2nage[/eluser]
I have tried to change the "uri_segment" value but no improvements...

Best,

Bosse
#6

[eluser]bosse2nage[/eluser]
We found the error!

The $offset should be removed from the "base_url":

Code:
$config['base_url']=site_url("projects/display/$sort_by/$sort_order");

instead of

Code:
$config['base_url']=site_url("projects/display/$sort_by/$sort_order/$offset");

Thanks everyone for your help!!!




Theme © iAndrew 2016 - Forum software by © MyBB