Welcome Guest, Not a member yet? Register   Sign In
Help with Active Record please!
#1

[eluser]dpdp[/eluser]
Hi guys,

I know my way around CI pretty well, until it comes to relational tables. My lead developer had to take off for a day and I need to be able to query these results possibly.

I'll give you a rough outline of the table structure he has setup here

tbl_users (user data stored here) user_id
tbl_avatars (avatar filenames are in here with some other related info) avatar_id
tbl_avatars_users (he's using this to join the two together) user_id, avatar_id

So, whether he did that right or wrong isn't my main concern (but any input on that is appreciated). My situation is as follows.

Not all users have avatars. But I need to query the 30 latest users, and render the avatars, if they don't have an avatar, the default avatar is used. However! If I use join(tbl_avatars etc etc) I only get results consisting of the latest 30 users who HAVE avatars.

Is there a way to do this? Possibly without join?

You're help, in any fashion, is immensely apprecated. Needed it done 3 hours ago, hah!
#2

[eluser]n0xie[/eluser]
This isn't really related to Active Record, just basic SQL. What you probably need is a LEFT JOIN. Take a look here.
#3

[eluser]rogierb[/eluser]
yup, left join will do it

Code:
$this->db->join('tbl_avatars_users tau','tu.user_id  = tau.user_id','left');
Or something similar.
#4

[eluser]dpdp[/eluser]
You guys are the best, thank you!
Haha.
I can't believe I only had to add 'left' to what I already had.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB