views:

51

answers:

1

Hi,

I basically can’t get the pagination bit to work, I did before I changed my database query and now I’m stuck.

My model looks like:

function get_properties($limit, $offset) {
   $location = $this->session->userdata('location');
   $property_type = $this->session->userdata('property_type');
   if($property_type == 0) 
   {
      $sql = "SELECT * FROM properties ";
   }
   // more queries here
   $sql .= " LIMIT ".$limit.", ".$offset.";";
   $query = $this->db->query($sql);
   if($query->num_rows() > 0) {
      $this->session->set_userdata('num_rows', $query->num_rows());
      return $query->result_array();    
      return FALSE;
      }
   }
} 

and my controller looks like:

function results() {
   $config['base_url'] = base_url().'/properties/results';
   $config['per_page'] = '3';
   $data['properties_results'] = $this->properties_model->get_properties($config['per_page'], $this->uri->segment(3));
   $config['total_rows'] = $this->session->userdata('num_rows');
   $this->pagination->initialize($config);
   $config['full_tag_open']='<div id="pages">';
   $config['full_tag_close']='</div>';
   $data['links']=$this->pagination->create_links();
   $this->load->view('properties_results',$data);
} 

please help…its screwing up!

A: 

The reason it's not working is that you never get the total_rows. You get the total_rows via this query, but it already has an offset and a limit:

$sql .= " LIMIT ".$limit.", ".$offset.";";
$query = $this->db->query($sql);

To fix this you should add a function to your model:

function get_all_properties()
{
    return $this->db->get('properties');
}

Then in your controller, instead of:

$config['total_rows'] = $this->session->userdata('num_rows');

Do:

$config['total_rows'] = $this->properties_model->get_all_properties()->num_rows();

This should fix your pagination. Other than this your code has some strange things. E.g. return FALSE; in get_properties will NEVER execute. And why are you storing so much data in sessions. This is not necessary and not a good idea in my opinion.

captaintokyo
Its ok I have fixed it, it was to do with the total rows returned.The data in the sessions is from a search form, how else could I do this to pass information from page to page through codeigniter?
Ashley