akomaz Posted May 6, 2013 Share Posted May 6, 2013 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 More sharing options...
Barand Posted May 6, 2013 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 Link to comment https://forums.phpfreaks.com/topic/277726-what-is-the-problem-with-my-while-loop/#findComment-1428706 Share on other sites More sharing options...
RonnieCosta50 Posted May 6, 2013 Share Posted May 6, 2013 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. Link to comment https://forums.phpfreaks.com/topic/277726-what-is-the-problem-with-my-while-loop/#findComment-1428708 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); Link to comment https://forums.phpfreaks.com/topic/277726-what-is-the-problem-with-my-while-loop/#findComment-1428709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.