I have two table objects:
Users and Items
When the relations of these items is 1 to N (Each user can have many items).
Thus the Items table has a non-unique user_id field.
I want to present the information of all the users and their items on the page, like that:
User 1
--- Item 1
--- Item 2
--- Item 3
User 2
--- Item 1
--- Item 2
--- Item 3
How should I achieve this?
Should I loop through all the rows of the users and then per user fetch an Item row from their table? (Code)
$result = mysql_query('select * from users');
while($row = mysql_fetch($result))
{
....
$result2 = mysql_query('select * from items where user_id = ' . $row['user_id']);
while($item_row = mysql_fetch($result2))
{
.....
}
}
Or, should I do one big join of users and items and just go through it all?
Is there another common solution to this?