473,288 Members | 1,771 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,288 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 1915
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.