Jump to content

Formatting Javascript with MySQL queries.


SupremeBeing

Recommended Posts

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

<?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.

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

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.