akomaz Posted May 6, 2013 Share Posted May 6, 2013 (edited) 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 May 6, 2013 by akomaz Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted May 6, 2013 Solution Share Posted May 6, 2013 In the first iteration you read through all the rows in the mysql result set. In subsequent iterations there are no more rows to read. You can reposition at the start with data_seek function Quote Link to comment Share on other sites More sharing options...
RonnieCosta50 Posted May 6, 2013 Share Posted May 6, 2013 (edited) 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 May 6, 2013 by RonnieCosta50 Quote Link to comment Share on other sites More sharing options...
akomaz Posted May 6, 2013 Author Share Posted May 6, 2013 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); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.