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

(This post was last modified: 08-28-2018, 04:57 AM by HarrysR.)

Hey guys,
I've created the pagination in codeigniter but i have a problem on getting the entries. While in the first page as i saw it gets the proper entries, when i change the page i can not see the right ones. I also placed a date function in order to check the creation date of the entries and as i see the further i go  in the pagination, the entries are not getting older (if you know what i mean).

Moreover if the entries are less than the "per_page" value then i keep getting pagination links in the view file.

I also have a "GET" filtering function for the entries.



[EDIT]: Changed it a bit but still not working as i wish. I get in the second page and i don't get a thing.. All i get is an empty page, like it's not getting any data.



Here's my controller file:
Code:
public function index(){

    $query = $this->db->get('pets');
    $pet_num = $query->num_rows();

    $config = array();

    $config["base_url"] = base_url() . "pets/index/";
    $config["total_rows"] = $pet_num;
    $config["per_page"] = 12;
    $config['use_page_numbers'] = TRUE;
    $config['reuse_query_string'] = TRUE;

    $config['attributes'] = array('class' => 'page-link');
    $config["full_tag_open"] = '<ul class="pagination">';
    $config["full_tag_close"] = '</ul>';
    $config["first_link"] = "&laquo;";
    $config["first_tag_open"] = "<li>";
    $config["first_tag_close"] = "</li>";
    $config["last_link"] = "&raquo;";
    $config["last_tag_open"] = "<li>";
    $config["last_tag_close"] = "</li>";
    $config['next_link'] = '&gt;';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '<li>';
    $config['prev_link'] = '&lt;';
    $config['prev_tag_open'] = '<li class="page-item">';
    $config['prev_tag_close'] = '<li>';
    $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

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

    $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;

    $limit = $config['per_page'];
    $start = $page * $config['per_page'];

    $data['pets'] = $this->pet_model->get_filtered_pets($limit, $start);

    $data["links"] = $this->pagination->create_links();

    //View files
    $this->load->view('templates/header', $data);
    $this->load->view('pets/index', $data);
    $this->load->view('templates/footer', $data);
  }

My model:
Code:
   public function get_filtered_pets($limit, $start){
    $this->db->order_by('pet_id', 'DESC');
    $this->db->join('users', 'users.user_id = pets.pet_user_id');
    $this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');

    $pet_data = array();
    if ( $this->input->get('petStatus') ) {
      $pet_data['pet_status'] = $this->input->get('petStatus');
    }
    if ( $this->input->get('petCategory') ) {
      $pet_data['category_id'] = $this->input->get('petCategory');
    }
    if ( $this->input->get('petGender') ) {
      $pet_data['pet_gender'] = $this->input->get('petGender');
    }
    if ( !empty($pet_data) ) {
      $this->db->where($pet_data);
    }

    $this->db->limit($limit, $start);
    
    $query = $this->db->get('pets');

    return $query->result_array();

  }


Any ideas?!

Thanks!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#2

(This post was last modified: 08-27-2018, 12:32 PM by Wouter60.)

Check the query (in your model):
PHP Code:
if ( !empty($pet_data) ) {
    
$this->db->where($pet_data);
}
$query $this->db->get('pets');
if (
$start 0) {
    echo 
$this->db->last_query();
     die();
}
return 
$query->result_array(); 
Reply
#3

(08-27-2018, 09:00 AM)Wouter60 Wrote: Check the query (in your model):
PHP Code:
if ( !empty($pet_data) ) {
    
$this->db->where($pet_data);
}
$query $this->db->get('pets');
if (
$start 0) {
    echo 
$this->db->last_query();
     die();
}
return 
$query->result_array(); 

All i get is my query. "SELECT * FROM "table" JOIN etc... etc..."

And everything looks ok..

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#4

(This post was last modified: 08-28-2018, 04:25 AM by InsiteFX.)

He's calling db limit after he is getting the database record.
He should call db limit before calling the database for a record.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(08-28-2018, 04:25 AM)InsiteFX Wrote: He's calling db limit after he is getting the database record.
He should call db limit before calling the database for a record.

I've added the limit before the data call and still nothing.

Code:
   if ( !empty($pet_data) ) {
     $this->db->where($pet_data);
   }

   $this->db->limit($limit, $start);
   
   $query = $this->db->get('pets');

   return $query->result_array();

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#6

Well i think i managed to solve it by calculating the $limit and $offset in my controller.
As you can see here https://pastebin.com/7pGj9dnQ

A last thing i wanna ask and might be a silly one so be kind Tongue  (not necessary when there will be lots of entries but i wanna figure it out) is, what if i have only one page after filtering the data with the url parameters?! 

E.g. i filter the data, i only get 12 entries (as the per_page configuration) and i still get pagination links in the bottom even if the second page is empty.

Any ideas on that?!

Thanks!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#7

Controller (after $data["links"] = $this->pagination->create_links()):
PHP Code:
if ($pet_num <= $config['per_page']) $data['links'] = NULL
Reply
#8

(08-28-2018, 08:10 AM)Wouter60 Wrote: Controller (after $data["links"] = $this->pagination->create_links()):
PHP Code:
if ($pet_num <= $config['per_page']) $data['links'] = NULL
Nope.. still the same..

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#9

I checked a site of my own that uses the pagination library.

You have:
PHP Code:
$start $page $config['per_page']; 

And you use $start as the offset.
In my website, I use $page as the offset, which works fine.

Try if you get the correct results if you do this:
PHP Code:
//$start = $page * $config['per_page'];
$start $page
Reply
#10

(08-28-2018, 10:35 AM)Wouter60 Wrote: I checked a site of my own that uses the pagination library.

You have:
PHP Code:
$start $page $config['per_page']; 

And you use $start as the offset.
In my website, I use $page as the offset, which works fine.

Try if you get the correct results if you do this:
PHP Code:
//$start = $page * $config['per_page'];
$start $page


I got the right results (as i can see) by making this:
Code:
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 1;

$start = ( $page - 1 ) * $config['per_page'];


But the major problem is that when i filter the data and theoretically some of them may be less than the per_page config, i keep getting pagination links shown..

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB