简体   繁体   中英

How to set values in JavaScript (Highcharts) using PHP

I have the following present in chart.js :

xAxis: {
    categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},

I have a table called monthly_flagged which stores the total amount of flagged posts for each month. I am trying to create a line chart to show the fluctuation between number of flagged posts every month.

Using PHP I have got the following data:

$statement = mysqli_prepare ($connect, "SELECT * FROM monthly_flagged");
mysqli_stmt_execute($statement);
$get_data = mysqli_fetch_assoc ($statement);
    $month          = $get_data['month'];
    $year           = $get_data['year'];
    $total_flaggged = $get_data['total_flagged'];
mysqli_stmt_close($statement);

The thing is, I am unsure on how I can apply this data to JS. Assume my monthly_flagged table has one row:

id:1
month: April
year: 2016
total_flagged:28

Using the data obtained in $month and $total_flagged , I want to update the Apr on my x-axis.

Currently, this is how data is displayed (using test numbers):

series: [{
            name: 'Flagged posts',
            data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        } 

But I basically need it to go something like this:

series: [{
            name: 'Flagged posts',
            data: [$monthJan, $monthFeb, $monthApr, ...]
        }

But again, unsure how to do this.

Try storing your data in an array,

$statement = mysqli_prepare ($connect, "SELECT * FROM monthly_flagged");
mysqli_stmt_execute($statement);
$months = array();
$data = array();
$result = mysqli_stmt_get_result($statement);
while($get_data = mysqli_fetch_assoc ($result)){
    $months[]          = $get_data['month'];
    $data[]            = $get_data['total_flagged'];
}
mysqli_stmt_close($statement);

Then on your javascript:

xAxis: {
    categories: [<?php echo json_encode($months); ?>]
},

And finally:

series: [{
            name: 'Flagged posts',
            data: [<?php echo json_encode($data); ?>]
        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM