SupremeBeing Posted February 2, 2011 Share Posted February 2, 2011 Hello Everyone, I think this might be a bit of a challenge but its always worth getting some input for solutions. I'm using jqplot to create graphs from data held in a database. And im having trouble thinking of a way to lay the data out in an acceptable way. To plot a line the javascript takes a statement in this form: line1 = [['x-axis1'],y-axis1],['x-axis2',y-axis2] I'm running this query to get the data: <?php $query2 = "SELECT * FROM tracker WHERE username = '" . mysql_real_escape_string($usera) . "'"; $result = mysql_query($query2) or die('Error, query failed : ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { $data[] = $row; } ?> This returns things like this: $data[0]['date'], $data[0]['reps'] $data[1]['date'], $data[1]['reps'] etc... Values are likely to be added or removed so I need to construct a loop of some kind to format it like this, line1 = [['{$data[0]['date']}',{$data[0]['reps']}],['{$data[1]['date']}',{$data[1]['reps']}],['{$data[2]['date']}'],{$data[2]['reps']}]]; Can someone point me in the right direction? Thanks in Advanced Quote Link to comment Share on other sites More sharing options...
radi8 Posted February 2, 2011 Share Posted February 2, 2011 <?php $line1=""; $1stTime=true; $query2 = "SELECT * FROM tracker WHERE username = '" . mysql_real_escape_string($usera) . "'"; $result = mysql_query($query2) or die('Error, query failed : ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { // handle the comma insertion, not initially and not at the end if(!$1stTime) $line1 .= ","; $line1.= "[['{".$row['date']."}',{".$row['reps']."}]"; } ?> This should build the string, or get you close. Quote Link to comment Share on other sites More sharing options...
SupremeBeing Posted February 2, 2011 Author Share Posted February 2, 2011 That was pretty much spot on. Thank alot radi8. Heres my final code <?php include 'cp/config.php'; include 'cp/opendb.php'; $line1=""; $stTime=true; $query2 = "SELECT * FROM tracker WHERE username = '" . mysql_real_escape_string($usera) . "'"; $result = mysql_query($query2) or die('Error, query failed : ' . mysql_error()); while($row = mysql_fetch_assoc($result)) { // handle the comma insertion, not initially and not at the end if(!$stTime) $line1 .= ","; $line1.= "['".$row['date']."',".$row['reps']."],"; } ?> //Javascript: line1 = [$line1]; Thank you so much for pointing me this way, I understand completly 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.