|
Posted by frothpoker on 08/31/06 09:03
two queries would be easier but obviously has a bigger overhead.
You could do the following:
Set $CurrentYear as the yearID from the first row
on each time through the loop, check YearID against $CurrentYear and if
it is not the same then end the table and start a new one before
setting $CurrentYear to the yearID of the current row.
tzuriel wrote:
> Hello all,
>
> I think nested loops will do what I want, but I can't seem to get them
> right.
>
> I have two tables, members and casts. I run a query as follows:
>
> $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 FROM
> casts WHERE memberID='$id')) AND ((casts.memberID)<>'$id'));";
>
>
> Which gets me the data I want to display using the following php:
>
> $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 $result
> print "<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>
>
> However, the data is just one long list in the table with the two
> columns Cast Member and Season. What I want to do is have a separate
> table 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 one
> table and those members from 1981 are in another.
>
> Can this be done with nested loops? Do I need another query? Please
> help if you can. I've struggled with this for a couple days and being
> very new, have run out of stuff to try!
>
> Thanks!
>
> Tzuriel
[Back to original message]
|