0

I retrieve information from php in Json form to pass it in my chart which is working by javascript and i already pass from php to javascript correctly, but when I use this javascript in the chart, the chart stop working

$result = mysql_query("select * from employee", $db);  

//Create an array
$json_response = array();

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

    $row_array['date'] = $row['date'];
    $row_array['rate'] = $row['rate'];


    //push the values in the array
    array_push($json_response,$row_array);
} 
$all= json_encode($json_response);
echo($all);

//Close the database connection
mysql_close($db);

?>


<script >
    var data_from_php = '<?php echo $all; ?>';
    alert(data_from_php);


    var  data = [{
             "date":"2012-03-01",
            "rate":"6"
        }, {
            "date": "2012-03-02",
            "rate": "-10"
        }, {
            "date": "2012-03-03",
            "rate": "1"
        },
        {
            "date": "2012-03-03",
            "rate": "1"
        },
        {
            "date": "2012-03-04",
            "rate": "5"
        },
        {
            "date": "2013-03-05",
            "rate": "10"
        },
        {
            "date": "2013-04-03",
            "rate": "-5"
        },
        {
            "date": "2013-05-10",
            "rate": "-2"
        }]



    var chart = AmCharts.makeChart("chartdiv", {
        "type": "serial",
        "theme": "light",
        "marginTop":0,
        "marginRight": 80,
        "pathToImages": "http://www.amcharts.com/lib/3/images/",
        "dataProvider": data,
        "valueAxes": [{
            "axisAlpha": 0,
            "position": "left"
           }],

well , I used data manual in Json form to see if the chart is working and I alert the variable which I recieved it from php and convert it to Javascript and all working good

enter image description here


enter image description here

enter image description here

Rich
  • 5,603
  • 9
  • 39
  • 61
karim_mis
  • 53
  • 1
  • 9
  • 2
    Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 01 '15 at 11:50

1 Answers1

3

change

var data_from_php = '<?php echo $all; ?>';

to

var data_from_php = <?php echo $all; ?>;
Vishal Wadhawan
  • 1,085
  • 1
  • 9
  • 11