i want to show data from table_one,where tableone_id is included on table_two..which is one to many relationship..here is the example.
table_one table_two id | name id | name |tableone_id 1 | A 1 | C | 1 2 | B 2 | D | 1 3 | E | 2 4 | F | 2The result i was expecting on my php is..
Number | Name | Linked Item | 1 | A | C | | | D | 2 | B | E | | | F |i already tried some code like
**Controller.php** $head = $this->db->query("SELECT * from table_one)->result_array(); foreach($head as $key => $value) { $head[$key]['items'] = $this->db->query("SELECT a.id, b.id, b.name as tabletwo_name, FROM table_one a JOIN table_two b on a.id = b.id where b.id =".$value['id'])->result_array(); }the code i make ,fails.how did i do the right code for that case ?
Thank you
your db queries should be in the model, not in the controller you are missing double quotes in the first select, your string is not closed why are you selecting all results from table_one and then doing JOINed select? You are not even selecting the data you want to have in your output (like table_two.name).Change your first query to:
$this->db->query("SELECT table_one.id as number, table_one.name as name, table_two.name as linked_item FROM table_one LEFT JOIN table_two ON table_one.id = table_two.tableone_id")->result_array();and then just do this:
foreach($head as $key => $value) { $head[$key]['items'] = $value; }