Forum Moderators: coopster

Message Too Old, No Replies

problem with loops and embedded loops

         

amikin

7:41 pm on Feb 28, 2007 (gmt 0)

10+ Year Member



if($numClasses){
while($ids = mysql_fetch_array($classRslt)){
if($numInput){
while($iids = mysql_fetch_array($inputRslt)){
echo $iids['class_id']." / ";
echo $ids['id']." ".$ids['program_name']." / Room: ".$ids['room_name']."<br>";
}
}
}
}

So $classRslt has 3 records to loop through, $inputRslt has 2.
The first while loops starts using the first record from $classRslt, then hits the embedded while loop and runs through both records of $inputRslt.

So my output ends up with 2 lines containing the 2 unique class_id and the id, program_name, and room name of the first record in $classRslt then it stops and doesn't proceed with the 2nd and 3rd iterations from my $classRslt loop

What am I doing wrong?

amikin

7:43 pm on Feb 28, 2007 (gmt 0)

10+ Year Member



Forgot to mention what I'm eventually trying to achieve with this.

I need to do a compare on the $iids['class_id'] and $ids['id'] and spit out any values that do not match.

cameraman

8:04 pm on Feb 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The recordsets don't automatically "rewind" - once the data's been read, it's finished.
Personally I would read them both into arrays, then iterate through the arrays, but if you have a reason to leave them in the tables, you can rewind the inner dataset when it breaks out of its while with:
mysql_data_seek($inputRslt,0)

amikin

8:16 pm on Feb 28, 2007 (gmt 0)

10+ Year Member



that's the missing link.

i'm new at php so wasn't too sure how to accomplish it, but what you are saying about using the arrays instead is making sense

Thanks

amikin

9:50 pm on Feb 28, 2007 (gmt 0)

10+ Year Member



OK, being new to php, I'm still fighting with this.

$ids = array();
while($a = mysql_fetch_array($classRslt)){
array_push($ids,$a);
}
$iids = array();
while($a = mysql_fetch_array($inputRslt)){
array_push($iids,$a);
}
for($i=0;$i<$numClasses;$i++){
for($b=0;$b<$numInput;$b++){
if($ids[$i]['id']!= $iids[$b]['class_id']){
echo $iids[$b]['class_id']." / ";
echo $ids[$i]['id']." ".$ids[$i]['program_name']." / Room: ".$ids[$i]['room_name']."<br>";
}
}
}

It doesn't quite work the way I need it to, this is what gets returned:
20 / 18 18Program / Room: 18Room
19 / 18 18Program / Room: 18Room
20 / 19 19Program / Room: 19Room
19 / 20 20Program / Room: 20Room

What I need to be echoed is just the 18Program once, since it's the only id that doesn't match one of the class_id being output from $iids

I'm not sure how to accomplish that, any suggestions?

cameraman

11:46 pm on Feb 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Easy task for php (the language that just keeps on giving <grin>):

for($i=0;$i<$numClasses;$i++) {
if(!in_array($id[$i],$iid))
echo 'This class isn\'t in a room: ' . $id[$i] . '<br />';
}
for($b=0;$b<$numInput;$b++) {
if(!in_array($iid[$b],$id))
echo 'This room doesn\'t have a class: ' . $iid[$b] . '<br />';
}

You also don't have to use array_push - there's nothing wrong with it, but you could also use:
$ids[] = $a;