Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
01-25-2013, 10:27 AM #1
- Join Date
- Sep 2012
- Thanked 0 Times in 0 Posts
Looping through two tables to produce a list?
Hello everyone, I am looking to use two tables within a query and output the results in php.
I have one table which holds details about a group and is called 'groups' with these columns
I have another table called 'people' who belong to these groups with these columns
What I'm looking to do is produce a list in php where I can loop through all the 'groups' and output each heading, then directly underneath that heading output all the their_name that are part of that particular group from the 'people' table.
Can anyone help here please. At the moment with my scenario I can't even think of the right terms to use a search engine for to find some similar scenarios which would help me, so even anything like that would be helpful. Thank you
Last edited by Oatley; 01-25-2013 at 06:00 PM.
01-25-2013, 02:30 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,650 Times in 2,619 Posts
Join and sort. Display is where it can get tricky since you don't really describe what you want the output to look like.
$sql = "SELECT t1.their_name, t2.heading, t2.group_id FROM table1 t1 INNER JOIN table2 t2 ON t2.group_id = t1.group_id ORDER BY t2.group_id ASC";
if ($qry = mysql_query($sql))
$iLastGroup = 0;
while($row = mysql_fetch_assoc($qry))
if ($iLastGroup != $row['group_id'])
if ($iLastGroup > 0)
$iLastGroup = $row['group_id'];
header('HTTP/1.1 420 Enhance Your Calm');
Users who have thanked Fou-Lu for this post: