473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Nested loops help please?

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

Aug 31 '06 #1
3 1918
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
Aug 31 '06 #2
Thanks for the suggestion. Would you be able to offer some code for
consideration? I tried something like this at one point but couldn't
get it to work.

Thanks,

Tzuriel
frothpoker wrote:
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.
Aug 31 '06 #3
// your query
$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'));";

$CurrentYear = 0;

while($result=mysql_query($sql_query); )
{
if ($CurrentYear == $result[1])
{
$string = $string .
"</TABLE><P><TABLE><TR><TD>$result[1]</TD></TR>";
$CurrentYear = $result[1]; // sets $CurrentYear to the same as
this record
} //end if statement
$string = $string . "<TR><TD>$result[2]</TD></TR>";
} // end while statement

print $string
-- code ends --

Obviously you will need to output table headers before the first record
and put some checking in so that you don't close the table if this is
the first record in the loop, but if i did it all for you....

Please note that I have not checked this for spelling or syntax (i am
notorious for leaving semi-colons off...)

Obiron

tzuriel wrote:
Thanks for the suggestion. Would you be able to offer some code for
consideration? I tried something like this at one point but couldn't
get it to work.

Thanks,

Tzuriel
frothpoker wrote:
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.

Sep 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
10
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.