Jump to content

Archived

This topic is now archived and is closed to further replies.

tzuriel

Nested Loops help, please?

Recommended Posts

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 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 FROM
casts 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 $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>[/code]

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.  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?  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

Share this post


Link to post
Share on other sites
You have an error in that code

<?php
echo "Cast data for: <b>$castmember</b>"; ID:$id<br>";
?>
Should be
<?php
echo "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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
add to your qwery ORDER BY casts.yearID
then set $year1='-456'; or samting that is not posible
in 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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.