CodeIgniter Forums
Issue with joining tables. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Issue with joining tables. (/showthread.php?tid=71527)



Issue with joining tables. - HarrysR - 08-24-2018

Hey guys,
I know it might be a silly question but i can't make it work and it drives me crazy.

I have a view.php for displaying single item details. In model file i'm trying to join some tables (in which some columns are INT and have 0 value).

When I'm trying to get a single entry where the INT column is 0, i can't get any info shown in the "view.php" file (if i also place the show_404 if data are empty i get redirected. Which means that it does not get any data).
When i erase the db join it works fine. My guess is something i made wrong with the db or the model.

Any ideas?!
Controller
Code:
 public function view($pet_entry_id = NULL, $pet_entry_slug = NULL){
   //Data
   $data['pet'] = $this->pet_model->get_pets($pet_entry_id);
   $data['images'] = $this->pet_model->get_images($pet_entry_id);

   //Same page redir with entry-id given
   if (!$pet_entry_slug) {
     redirect(site_url('pets/' . $data['pet']['pet_entry_id'] . '/' . $data['pet']['pet_entry_slug']), 301);
   }

   if( empty($data['pet']) ){
     redirect('pets');
   }

   //Meta data
   $seo_data['title'] = $data['pet']['pet_entry_title'] . ' - Petgee.gr';
   $seo_data['meta_descr'] = $data['pet']['meta_descr'];
   $seo_data['meta_keywords'] = $data['pet']['meta_keywords'];

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



Model file
Code:
 public function get_pets($pet_entry_id = 0, $pet_entry_slug = FALSE){
   $this->db->join('users', 'users.user_id = pets.pet_user_id');
   $this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');
   $this->db->join('pet_breeds', 'pet_breeds.pet_breed_id = pets.pet_breed');

   if($pet_entry_id === 0 && $pet_entry_slug === FALSE ){
     $this->db->order_by('pet_id', 'DESC');

     $query = $this->db->get('pets');
     return $query->result_array();
   }

   $query = $this->db->get_where('pets', array('pet_entry_id' => $pet_entry_id, 'pet_entry_slug' => $pet_entry_slug));
   return $query->row_array();
 } //Get pets (end)



Thank you in advance!


RE: Issue with joining tables. - jreklund - 08-25-2018

I can't see that you are converting $pet_entry_id into an integer. It will be a string of '0' so the following will not be true.
PHP Code:
if($pet_entry_id === && $pet_entry_slug === FALSE ){ 



RE: Issue with joining tables. - HarrysR - 08-25-2018

(08-25-2018, 05:31 AM)jreklund Wrote: I can't see that you are converting $pet_entry_id into an integer. It will be a string of '0' so the following will not be true.
PHP Code:
  if($pet_entry_id === && $pet_entry_slug === FALSE ){ 

The problem is not the Pet_entry_id.. The problem is when the breed (which is optional) gets a zero value. If the value is different and not 0 them it works fine. I dont want that though, since as i mentioned is optional


RE: Issue with joining tables. - HarrysR - 08-25-2018

I solved it by creating a value in the db of id number 1 (for no value) since the problem was that the 0 value i was joining with the other table could not be found.

If anyone has any other solution to this feel free to explain!

Thank you!