Jump to content


Photo

Nested Loops help, please?


  • Please log in to reply
3 replies to this topic

#1 tzuriel

tzuriel
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 31 August 2006 - 05:38 PM

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:

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

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:

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

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

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 31 August 2006 - 05:43 PM

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
<?php
$query = "SELECT * FROM table";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result){
echo "Field name X is ".$row["x"]."<br />\n";
}
?>

Tell me the problem, I will try tell you the solution

#3 tzuriel

tzuriel
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 31 August 2006 - 05:49 PM

Thanks.  I think that part is already fixed and I pasted an older version.  I actually just deleted one double quote and semi-colon:

<?php echo "Cast data for: <b>$castmember</b> ID:$id<br>";?>

Any ideas on the looping question?

Thanks!

Tzuriel

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 31 August 2006 - 07:56 PM

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
if ($year != $year1){
$year1 = $year;
print "<tr><td colspan=\"2\">In $year, $castmember was in the cast with:</td></tr>";
}
i make litle changes




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users