Jump to content

What is the problem with my WHILE loop?


akomaz
Go to solution Solved by Barand,

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!

Edited by akomaz
Link to comment
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.

Edited by RonnieCosta50
Link to comment
Share on other sites

Thanks that did resolve the issue.  Here's the modified version:

$i = 2004;

	do {
		echo "data.addRow(['".$i."' ";
			$i++;
			mysql_data_seek($prd2, 0);
			while($row = mysql_fetch_array($prd2))
			{
			echo ",".$row[$i]." ";
			}
		echo "]);";
	} while ($i < 2020);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.