0

I want to make some charts with dxcharts. Now i have the following situation:

My SQL query is:

$sql = "SELECT
av.art_title,
SUM(CASE c.cont_status WHEN 'cold' THEN av.art_views ELSE 0 END) cold,
SUM(CASE c.cont_status WHEN 'lead' THEN av.art_views ELSE 0 END) lead,
SUM(CASE c.cont_status WHEN 'prospect' THEN av.art_views ELSE 0 END) prospect,
SUM(CASE c.cont_status WHEN 'lost' THEN av.art_views ELSE 0 END) lost
FROM article_views av
JOIN contacts c ON av.user_id = c.id
GROUP BY av.art_title";
$res = mysql_query($sql) or die (mysql_error());

I have a while loop on the query like:

while($row = mysql_fetch_assoc($res)) { }

And below i will post my javascript var:

var dataSource = [{ country: "<?php echo $row['art_title']; ?>", lead: <?php echo $row['lead']; ?>, lost: <?php echo $row['lost']; ?>}, ];

So if i use the while in the datasource var it's not working at all. Can anyone help me out here?

7
  • Are you using inline javascript? Commented Oct 28, 2014 at 20:23
  • 1
    You're dumping text from PHP into a JS context - you're vulnerable to the JS equivalent of sql injection attacks. ALWAYS output via json_encode() so you're generating syntactically correct JS strings. Commented Oct 28, 2014 at 20:24
  • is it a misspell art_titel ? instead of art_title Commented Oct 28, 2014 at 20:24
  • I have updated my code. But still not working. Commented Oct 28, 2014 at 20:28
  • It's inline javascript here, Colin. Commented Oct 28, 2014 at 20:28

2 Answers 2

1
$data=array();
while ($row = mysql_fetch_assoc($res)){
    $data[]=array('country'=> $row['art_title'], 'lead'=> $row['lead'], 'lost'=> $row['lost']);
}
$json=json_encode($data);

//javascript
var dataSource=<?php echo $json; ?>;
Sign up to request clarification or add additional context in comments.

8 Comments

It's not working. I have this in my javascript: var dataSource = <?php echo $json; ?> But i don't have the brackets [ ] Is that the problem?
What does the generated output look like? Are you getting any js errors I the console?
The chart is not showing up. No errors so far as i can see.
Now it's working here. The var datasource has to be a ; at the end. Steve, you forgot that. Do you have experience with dxcharts? I have a wrong sorting on the argumentaxis.
@BrechtS Oops, yes i missed that (typing code into a smartphone is not simple!). I dont have any direct experiance with dxcharts but if you explain your issue i might be able to help
|
0

By the way, the original MySQL extension is now deprecated http://php.net/manual/en/migration55.deprecated.php , and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MySQLi (or PDO_MySQL) extensions.

The E_DEPRECATED error will not be displayed probably on the production server, but it will be generated anyway and it takes time.

So the while loop will look like:

while ( $row = $result -> fetch_array ( MYSQLI_ASSOC ) ) { ... }

If you writing a new PHP application, - use the MySQLi.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.