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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.