0

I want to create a Flot graph that have on x-axis date values. I don't want to write manually the date values so I stored them in database like TIMESTAMP values. The connection to database is like:

<?php
  $pdo = new PDO('mysql:host=example.com; dbname=database', 'user', 'password')
?>

The javascript code looks like:

        var graphData = [ {
        // first graphic
        data: [ [(new Date("2006-06-12 19:13:37")).getTime(), 600], [(new Date("2006-06-21 19:13:37")).getTime(), 550], [(new Date("2006-07-1 19:13:37")).getTime(), 600]],
        color: '#77b7c5',
        points: { radius: 4, fillColor: '#77b7c5' }
    }, {
        //the second graphic
        data: [[(new Date("2006-06-11 19:13:37")).getTime(), 3210.196] , [(new Date("2006-07-3 19:13:37")).getTime(), 3310.196], [(new Date("2006-07-23 19:13:37")).getTime(),3875.422]],
        color: 'red'
    }       
];

I want to get those date values directly from my database and to create the graphic I use JavaScript. I know that is more secure to work with database in php and I don't know how I can display the flot graphic using values from database. Any ideas?

Diana C.
  • 1
  • 1
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – NullPoiиteя Aug 01 '13 at 09:17
  • Thank you, I edited the code and i used PDO instead, works great. – Diana C. Aug 01 '13 at 11:54
  • awesome .... that really good – NullPoiиteя Aug 01 '13 at 11:56
  • You'll have to [pass php variable(s) to javascript](http://stackoverflow.com/questions/1808108/how-to-access-php-variables-in-javascript-or-jquery-rather-than-php-echo-vari) – Paulo Almeida Aug 01 '13 at 12:05
  • Can you show the plot call? var plot = $.plot(placeholder, data, options) – Blake Aug 01 '13 at 13:44
  • Your question is a little to broad for this forum. Google "flot php json example" for tutorials. Here's one of the first hits: http://burnsforce.com/index.php/flot-format-data-flot-readable-json/ Seems like a good tutorial. – Mark Aug 01 '13 at 14:06

0 Answers0