CodeIgniter Forums
Similar entries function. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: Similar entries function. (/showthread.php?tid=71623)



Similar entries function. - HarrysR - 09-06-2018

I'm trying to create a "similar entries" function in codeigniter but the code so far gets me an error..

Controller 
Code:
$data['similar'] = $this->pet_model->get_similar($pet_entry_id);


Model
Code:
 public function get_similar($pet_entry_id){
   $this->db->select('category_id');
   $this->db->from('pets');
   $this->db->where('pet_entry_id', $pet_entry_id);
   $pet_type = $this->db->get();

   $this->db->select('pet_status');
   $this->db->from('pets');
   $this->db->where('pet_entry_id', $pet_entry_id);
   $pet_status = $this->db->get();

   $query = $this->db->get_where('pets', array('category_id' => $pet_type, 'pet_status' => $pet_status) );
   return $query->result_array();
 }

The php error shows that these values are blank:
Code:
SELECT * FROM `pets` WHERE `category_id` = AND `pet_status` =


I think that the issues comes from the queries... But where..?!?! Tongue


RE: Similar entries function. - Wouter60 - 09-06-2018

$this->db->get() does not return a field value from your table, but just a database object.
To get the value of the field:
PHP Code:
$pet_type $this->db->get()->row()->pet_type;
$pet_status $this->db->get()->row()->pet_status



RE: Similar entries function. - HarrysR - 09-06-2018

(09-06-2018, 07:58 AM)Wouter60 Wrote: $this->db->get() does not return a field value from your table, but just a database object.
To get the value of the field:
PHP Code:
$pet_type $this->db->get()->row()->pet_type;
$pet_status $this->db->get()->row()->pet_status

I think it works fine! Thank you!