How to pass a variable to a conditional join in codeigniter?

sorry earlier if my english is bad

first i have a variable like this

$data[code] = "xyz123"; $data[type] = "train"; 

and the request that I write in the model, for example

 $this->db->select('*'); $this->db->from('table1'); $this->db->join('table2', 'table2.id = table1.id'); $this->db->select('table3', 'table3.id = table1.id AND table1.code LIKE $data[code] AND table1.type LIKE $data[type]'); 

But when I try, its not working, and I think that the AND and LIKE syntax I am writing incorrectly. Does anyone know a solution?

Thank you before.

+4
source share
3 answers

I think you need to try:

 $this->db->select('table1.*,table2.*,table3.*'); $this->db->from('table1'); $this->db->join('table2', 'table2.id = table1.id'); $this->db->join('table3', 'table3.id = table1.id'); $this->db->like('table1.type', $data['type']); $this->db->like('table1.code', $data['code']); 
+3
source

Try a function like CI:

 $this->db->like('type', $data['type']); $this->db->like('code', $data['code']); 
+1
source
 $data["code"] = "xyz123"; $data["type"] = "train"; $this->db->select('*'); $this->db->from('table1'); $this->db->join('table2', 'table2.id = table1.id'); $this->db->select('table3', 'table3.id = table1.id AND table1.code LIKE '.$data["code"].' AND table1.type LIKE '.$data["type"].''); 
0
source

Source: https://habr.com/ru/post/1499303/


All Articles