phpdragon Posted February 9, 2009 Share Posted February 9, 2009 I have Table A, with columns Aid,Aname, I also have table B with columns Bid,Bname,Aid My question is how do I display Aname as headings, with a list of Bname(s) under each linked Aname using the Aid eg Aname_1(Aid1) Bname_1 Bname_2 Bname_7 Bname_32 Aname_2(Aid(2) Bname_3 Bname_4 Bname_9 Bname_12 etc etc etc Link to comment https://forums.phpfreaks.com/topic/144482-solved-how-to-display-result-as-headings-and-related-results-as-sub-headings/ Share on other sites More sharing options...
printf Posted February 9, 2009 Share Posted February 9, 2009 Examples... way to do it (1) multi query $q = mysql_query ("SELECT Aid, Aname FROM Table1 ORDER BY Aid;" ); if ( mysql_num_rows ( $q ) > 0 ) { while ( $r = mysql_fetch_assoc ( $q ) ) { echo "<h3>" . $r['Aname'] . "</h3><br/>"; $s = mysql_query ("SELECT Bname FROM Table2 WHERE Aid = '" . $r['Aid'] . "' ORDER BY Bid;" ); while ( $t = mysql_fetch_assoc ( $s ) ) { echo $t['Bname'] . "<br/>"; } } } way to do it (2) single JOIN query $q = mysql_query ("SELECT t1.Aid, t1.Aname, t2.Bname FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON (t1.Aid = t2.Aid) ORDER BY t1.Aid;" ); if ( mysql_num_rows ( $q ) > 0 ) { $name = ''; while ( $r = mysql_fetch_assoc ( $q ) ) { if ( $name != $r['Aname'] ) { echo "<h3>" . $r['Aname'] . "</h3><br/>"; $name = $r['Aname']; } echo $r['Bname'] . "<br/>"; } } Link to comment https://forums.phpfreaks.com/topic/144482-solved-how-to-display-result-as-headings-and-related-results-as-sub-headings/#findComment-758138 Share on other sites More sharing options...
phpdragon Posted February 9, 2009 Author Share Posted February 9, 2009 Thankyou so much printf, trying to get that function to work has really plagued me, I appreciate it Link to comment https://forums.phpfreaks.com/topic/144482-solved-how-to-display-result-as-headings-and-related-results-as-sub-headings/#findComment-758154 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.