Querying the database

#1
[eluser]Wonder Woman[/eluser]
Basically I want to take an input and pass it on to query the database and then bring back the results for the pagination. I want to query a location.
My controller looks like this:

Code:
function index() {
  if($this->validation->run()==FALSE){
    $this->load->view('property_view', $data);
  }
  else {
    $location = $this->input->post('location');
    redirect('property/results');
  }
}

function results() {
  $data['result']=$this->db->get('properties',10,$this->uri->segment(3));
  $config['base_url']=base_url().'/property/results';
  $config['total_rows']=$this->db->count_all('properties');
  $config['full_tag_open']='<div id="pages">';
  $config['full_tag_close']='</div>';
  $this->pagination->initialize($config);
        
  $data['links']=$this->pagination->create_links();
  $this->load->view('property_results',$data);
}


I figure this needs to be changed to put the query where 'properties' is because that is just requesting the database:

Code:
$data['result']=$this->db->get('properties',10,$this->uri->segment(3));

My model looks like:

Code:
function get_properties($num, $offset) {
  $query = $this->db->get('properties', $num, $offset);
  return $query;
}
    
function get_specified_location($location) {
  $this->db->like('town', $location);
  $query = $this->db->get('properties');    
  return $query->result_array();
}

#2
[eluser]pickupman[/eluser]
You are on the right track. A couple points to mention:
1.) When redirecting from your index function post() is only available on the page submit, so it's not being carried to your results page. Best set this in a users session
Code:
$this->session->set_userdata('location', $location);

2. You have to tell the pagination class how many records you want in each row
Code:
$config['per_page'] = 25;

3. You need to define your default offset.
Code:
$data['result'] = $this->db->get('properties',$config['per_page'] , $this->uri->segment(3,0));

4. Now that you have the person location stored in there session, you will need to update the model to reflect this.
Code:
function get_properties($num, $offset) {
  $location = $this->session->userdata('location');
  if(!empty($location)) return FALSE; //no location to search (maybe not necessary)

  $this->db->where('location', $location);
  $query = $this->db->get('properties', $num, $offset);
  if($query->num_rows() > 0)
     return $query->result(); //return result object rather than query object
  
   return FALSE; //No results
}

You will find later on that returning result objects will be better off. And adding the return FALSE will help checking the model call, before processing any further.

#3
[eluser]Wonder Woman[/eluser]
This is truly brilliant, thank you so much!

I was just wondering one thing though, because I want query the database using a function in my model:
Code:
function get_properties($limit) {
   $location = $this->session->userdata('location');
   if(!empty($location)) return FALSE;
        
   $this->db->like('town', $location);
   $query = $this->db->get('properties', $limit);
   if($query->num_rows() > 0) {
      return $query->result();
   }
   else {
      return FALSE;
   }
}

with the controller looking like this:

Code:
function results() {
   $config['base_url']=base_url().'/property/results';
   $config['total_rows']=$this->db->count_all('properties');
   $config['per_page'] = 5;
   $config['full_tag_open']='<div id="pages">';
   $config['full_tag_close']='</div>';
   $data['result']=$this->property_model->get_properties($config['per_page']);
   $this->pagination->initialize($config);
   $data['location'] = $this->session->userdata('location');
   $data['links']=$this->pagination->create_links();
   $this->load->view('property_results',$data);
}


How do I add this:

Code:
$this->uri->segment(3,0)

into:

Code:
$data['result']=$this->property_model->get_properties($config['per_page']);

Thanks so much for all of your help so far! Smile

#4
[eluser]pickupman[/eluser]
Well, I think I already had shown that in the previous post. You could modify it to:
Code:
function get_properties($limit = 25, $offset = 0) {
   $location = $this->session->userdata('location');
   if(!empty($location)) return FALSE;
        
   $this->db->like('town', $location);
   $query = $this->db->get('properties', $limit, $offset);
   if($query->num_rows() > 0)
     return $query->result();  
  
   return FALSE; // Extra curly braces not needed. A return ends execution of a function
  
}

//Controller
$offset = $this->uri->segment(3,0); //Set $offset to third segment or 0 if no 3rd segment
$data['result'] = $this->property_model->get_properties($config['per_page'], $offset);

The above example uses shorthand. If you only have one line after a if/else you do not need curly braces. Also there is no need of a else statement. If rows are found the the function will return the result object and end execution, or it will return false by default.

#5
[eluser]Wonder Woman[/eluser]
Got it working, I was being dumb! Thanks ever so much for your help and explanations! Yay! Smile

#6
[eluser]pickupman[/eluser]
Congrats!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.