Jump to content

What is the problem with my WHILE loop?


akomaz

Recommended Posts

I have created this WHILE loop inside a javascript function to pull a range of data from corresponding mysql records.

 

It is supposed to grab the year header and range of values from 2005-2020 from that record and output to the page

	$i = 2004;

	do {
		echo "data.addRow(['".$i."' ";
			$i++;
			while($row = mysql_fetch_assoc($prd2))
			{
			echo ",".$row[$i]." ";
			}
		echo "]);";
	} while ($i <= 2020);

The resulting javascript should look like this:

data.addRow(['2005', 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000]);

data.addRow(['2006', 10000, 5000, 7000, 28000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000, 12000, 8000, 9000, 25000]);

But the loop terminates after the first iteration.

 

Any ideas where my loop logic may have faultered here?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/277726-what-is-the-problem-with-my-while-loop/
Share on other sites

I think your error is in this line of code.

while($row = mysql_fetch_array($prd2))

 

Try this

while($row mysql_fetch_array($prd2, $i))

 

Or this

if($row mysql_fetch_array($prd2, $i))

 

I think your suppose to use an if statement in your case.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.