Forum Moderators: coopster

Message Too Old, No Replies

Cannot get nested for/while to work properly

         

natbur

8:12 pm on Jun 2, 2007 (gmt 0)

10+ Year Member



I feel like this should be extremely simple, but I cannot for the life of me get it to cooperate. Part of one of my functions pulls data out of a database and build an array, but the index variable doesn't seem to be available within the while loop.
for($i = 0; $i < $numAlts; $i++)
{
while($row = $db->sql_fetchrow($result))
{
echo $i;
$tempArray[($row['name'].$i)] = array('name' => $row['name'].$i, 'value' => $row['value'], 'show' => $row['show'], 'required' => $row['required']);
}
array_push($array, $tempArray);
}

the echo was put in there to help me debug the thing. It should be echoing 123... but instead it echo's 00000000.
The nested loops work fine, exiting when they should, but every row has the same name eg Name0.

Is there some weird PHP variable scoping that I'm not aware of?

natbur

8:17 pm on Jun 2, 2007 (gmt 0)

10+ Year Member



Nevermind, i found the issue, now i feel like an idiot...

natbur

8:24 pm on Jun 2, 2007 (gmt 0)

10+ Year Member



Since I didn't reinitialize $result, the while loop only ran once. However, since I never initialized my tempArray to empty, I kept pushing the same array into array, tricking myself into thinking that everything was working correctly since I ended up with the proper number of subarrays.