Forum Moderators: coopster

Message Too Old, No Replies

Nested loops driving me loopy

Can't get the inner loop to behave...

         

2oddSox

10:19 am on May 4, 2007 (gmt 0)

10+ Year Member



Hi there,

If you can help, I'd appreciate it.

I'm trying to do a nested 'do while' but can't seem to get the iteration working right (to be honest I'm not even sure that this is the best solution for what I need to do).

I have two tables with a common field, and (simplified), the code I have is this:

do {

if (condition is met) {
echo $maintopic;
}

do {
if (condition is met) {
echo $subtopic;

}

} while ($recordset1 = mysql_fetch_assoc($recordset1));

} while ($recordset2 = mysql_fetch_assoc($recordset2));

It runs the outer loop fine, but the inner loop only runs once. Rs1 pulls all the subtopics from one table, and Rs2 pulls all the main topics from another.

The idea is to get something like:


Main Topic 1
Sub topic 1
Sub topic 2
Sub topic 5

Main Topic 2
Sub topic 3
Sub topic 4
Sub topic 8


But what I'm getting is:


Main Topic 1
Sub topic 1
Sub topic 2
Sub topic 5

Main Topic 2
Main Topic 3
Main Topic 4


Any thoughts would be appreciated.

Thanks,

2odd...

vincevincevince

10:30 am on May 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



do {

if (condition is met) {
echo $maintopic;

do {
if (condition is met) {
echo $subtopic;

}

} while ($recordset1 = mysql_fetch_assoc($recordset1));
}

while ($recordset2 = mysql_fetch_assoc($recordset2));

Does that help? One loop must be entirely inside the other

2oddSox

10:42 am on May 4, 2007 (gmt 0)

10+ Year Member



Hi Vince,

Thanks for the response, but I get the same result. That's a handy tip to know tho'.

vincevincevince

10:48 am on May 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure it's a good answer, but I'd change it to a while loop:


while ($result1=mysql_fetch_assoc($query1))
{
print "main title";
$query2="select from..... where section=$result1[section]";
while ($result2=mysql_fetch_assoc($query1))
{
print "sub title";
}
}

2oddSox

12:26 pm on May 4, 2007 (gmt 0)

10+ Year Member



Thanks for trying Vince :)

I can see where you're going with the while loops, but my head just can't quite get around this right now. Friday afternoon and all that.

I'll see if some amber liquid a little later on can shed some light on this ;)

jatar_k

1:23 pm on May 4, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



vince3's while loop should be the ticket, play with that a little

try just building the outside loop, make that work

then I usually insert the query first using the data from the first query, so

while ($result1=mysql_fetch_assoc($query1)) {
print "main title";
$query2="select from..... where section=$result1[section]";
echo '<p>',$query2;
}

this way I can see what's going on and make sure I've properly constructed the queries

2oddSox

2:14 pm on May 4, 2007 (gmt 0)

10+ Year Member



Thanks Jatar_k,

I'm going to try Vince's way a little later on but start it all over from scratch and, as you say, construct it one bit at a time and echo it out to see what the heck I'm doing wrong.

Tell you what though, if anyone ever needs an infinite loop created, I'm your man.

jatar_k

2:15 pm on May 4, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> if anyone ever needs an infinite loop created, I'm your man.

thanks for the offer but I seem to be rather adept at it myself ;)

joelgreen

5:44 pm on May 4, 2007 (gmt 0)

10+ Year Member



I suspect your problem with do..while loop lays in "condition is met" clause and possibly $recordset1 initialization. Having "condsition i met" posted here would help.