tzuriel Posted August 31, 2006 Share Posted August 31, 2006 I think nested loops will do what I want, but I can't seem to get themright.I have two tables, members and casts. I run a query as follows which gets me the data I want to display with the following php:[code]<?php$sql_query="SELECT DISTINCT casts.yearID, members.memberName" . " FROM members INNER JOIN casts ON members.memberID =casts.memberID" . " WHERE (((casts.yearID) In (SELECT DISTINCT yearID FROMcasts WHERE memberID='$id')) AND ((casts.memberID)<>'$id'));";$result=mysql_query($sql_query);$num=mysql_numrows($result);echo "Cast data for: <b>$castmember</b>"; ID:$id<br>";$year =mysql_result($result,$i,"yearID");echo " 1st season: <b>$year</b><p>";//set up table for $resultprint "<table width=200 border=0>\n";print "\t<td><font face=arial size=1/><b>Cast Member</font></td>\n";print "\t<td><font face=arial size=1/><b>Season</font></b></td>\n";//loop through $result$i=0;while ($i < $num) { $member=mysql_result($result,$i,"memberName"); $year =mysql_result($result,$i,"yearID"); print "<tr>\n"; print "\t<td><font face=arial size=1/>$member</font></td>\n"; print "\t<td><font face=arial size=1/>$year</font></td>\n"; print "</tr>\n"; $i++;}print "</table>\n";?><?php echo "Cast data for: <b>",$castmember; ?><br></b>[/code]However, the data is just one long list in the table with the twocolumns Cast Member and Season. What I want to do is have a separatetable for each Season so that when yearID changes from, for example,1980 to 1981, the rows that show the cast members for 1980 are in onetable and those members from 1981 are in another. Something like this html:[code]<title>Query Results</title></head><html><body>Cast data for: Denny Dillon</b> ID:10<br> 1st season: 1980</b><p>In 1980, Denny Dillon was in the cast with:</p><table border="0" width="200"> <tbody><tr><td> Cast Member</b> </td> <td> Season</b> </td></tr><tr> <td> Gilbert Gottfried </td> <td> 1980 </td></tr><tr> <td> Gail Matthius </td> <td> 1980 </td></tr><tr> <td> Eddie Murphy </td> <td> 1980 </td></tr><tr> <td> Joe Piscopo </td> <td> 1980 </td></tr><tr> <td> Ann Risley </td> <td> 1980 </td></tr><tr> <td> Charles Rocket </td> <td> 1980 </table><p>In 1981, Denny Dillon was in the cast with:<p><table border="0" width="200"> <tbody><tr><td>Cast Member</td> <td>Season</td></tr><tr><td> Eddie Murphy </td> <td> 1981 </td></tr><tr> <td> Joe Piscopo </td> <td> 1981 </td></tr><tr> <td> Brian Doyle-Murray </td> <td> 1981 </td></tr><tr> <td> Robin Duke </td> <td> 1981 </td></tr><tr> <td> Christine Ebersol </td> <td> 1981 </td></tr><tr> <td> Mary Gross </td> <td> 1981 </td></tr><tr> <td> Tim Kazurinsky </td> <td> 1981 </td></tr><tr> <td> Tony Rosato </td> <td> 1981 </td></tr></tbody></table></body></html>[/code]Can this be done with nested loops? Do I need another query? Pleasehelp if you can. I've struggled with this for a couple days and beingvery new, have run out of stuff to try!Thanks!Tzuriel Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 31, 2006 Share Posted August 31, 2006 You have an error in that code<?phpecho "Cast data for: <b>$castmember</b>"; ID:$id<br>";?>Should be<?phpecho "Cast data for: <b>$castmember</b> ID:".$id."<br>";?>also to get results from a MySQL table[code]<?php$query = "SELECT * FROM table";$result = mysql_query($query);while($row = mysql_fetch_assoc($result){echo "Field name X is ".$row["x"]."<br />\n";}?>[/code] Quote Link to comment Share on other sites More sharing options...
tzuriel Posted August 31, 2006 Author Share Posted August 31, 2006 Thanks. I think that part is already fixed and I pasted an older version. I actually just deleted one double quote and semi-colon:[code]<?php echo "Cast data for: <b>$castmember</b> ID:$id<br>";?>[/code]Any ideas on the looping question?Thanks!Tzuriel Quote Link to comment Share on other sites More sharing options...
sasa Posted August 31, 2006 Share Posted August 31, 2006 add to your qwery ORDER BY casts.yearIDthen set $year1='-456'; or samting that is not posiblein your print loop after set variable do samthing ike this[code]if ($year != $year1){$year1 = $year;print "<tr><td colspan=\"2\">In $year, $castmember was in the cast with:</td></tr>";}[/code]i make litle changes Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.