Welcome Guest, Not a member yet? Register   Sign In
a little help with joining tables
#1

[eluser]ommy[/eluser]
Hi again,
Today i've finally seen the light.
Joining tables in codeigniter is pretty useful.
But i have a question for the all-knowing collective:

lets say i'm selecting the following:
Code:
people.name,
people.id,
country.id

how do i know, later on, which id i'm getting when i ask for $row->id?
And even more importantly, how can i get bolth?


Thanks in advance for the help.
Tommy
#2

[eluser]Jevedor[/eluser]
i believe the later reference to id will take precedence. I would recommend when you write your select you create a reference name like this

select a.id as first_id, b.id as second_id, ...


then the result will use first_id and second_id as the field values.

so in CI

Code:
$this->db->select('a.id as first_id, b.id as second_id')
$this->db->from('table_1 a');
$this->db->join('table_2 b', 'a.id = b.id', 'inner');
$result = $this->db->get();

$row = $result->row();
now you can access the values like this

Code:
$row->first_id;
$row->second_id;

hope that helps
#3

[eluser]ommy[/eluser]
Thanks a bunch!
I knew there was the 'as' way but had no idea how to use it.
helpful information indeed

On to the next problem Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB