FLOT data from MySQL via PHP?

2019-09-05 05:51发布

问题:

Good afternoon, I have looked at the FLOT website examples and googled but just cannot find anywhere with detailed steps on how to pull data from MySQL into FLOT.

Currently I have a php page with a recordset that stores all records from a mysql table, columns are date, user & cost. Do I need to create a data table to display all these records in the page?

When above is sorted how would I call that data into FLOT, I know its probably very obvious to any develiopers out there but I just cant find the info today.

Thanks for any help/pointers offered.

回答1:

Look the AJAX example in the flot documentation.

Essentially the steps are:

1.) Pull from database.

2.) Place data in PHP key/value array of form:

$dataSet1 = Array();
$dataSet1['label'] = 'Customer 1';
$dataSet1['data'] = Array(Array(1,1),Array(2,2)); // an array of arrays of point pairs

$dataSet2 = Array();
$dataSet2['label'] = 'Customer 2';
$dataSet2['data'] = Array(Array(3,3),Array(4,5)); // an array of arrays of point pairs

$returnArray = Array($dataSet1, $dataSet2);

3.) Back in your javascript, get this json encoded string as JS variable:

var data = <?php echo json_encode($arr); ?>;

4.) Back in your javascript, call the flot plot method with that data variable:

$.plot($("#placeholder"), data, options);


标签: mysql flot