将MYSQL SELECT转换为JSON以获取图表数据

时间:2017-01-19 13:52:09

标签: php jquery mysql

我正在选择一个表,它会在列中检查数据中的最高值,这个工作正常但是我需要将这些值转换为JSON,这样我就可以将这些值用于图表。 / p>

<?php


    $result = $conn->query("SELECT MAX(score_1) `top_score_1` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_1 =  $row['top_score_1'];
    }

    $result = $conn->query("SELECT MAX(score_2) `top_score_2` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_2 =  $row['top_score_2'];
    }

    $result = $conn->query("SELECT MAX(score_3) `top_score_3` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_3 =  $row['top_score_3'];
    }

    $result = $conn->query("SELECT MAX(score_4) `top_score_4` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_4 =  $row['top_score_4'];
    }

    $result = $conn->query("SELECT MAX(score_5) `top_score_5` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_5 =  $row['top_score_5'];
    }

    $result = $conn->query("SELECT MAX(score_6) `top_score_6` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_6 =  $row['top_score_6'];
    }

    $result = $conn->query("SELECT MAX(score_7) `top_score_7` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_7 =  $row['top_score_7'];
    }

    $result = $conn->query("SELECT MAX(score_8) `top_score_8` FROM members WHERE dashboard_id = $user_dash_id");      
    if (!$result) die($conn->error);
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)) 
    {
        $top_score_8 =  $row['top_score_8'];
    }                                                

    $total_five_stars = $top_score_1 + 
                        $top_score_2 + 
                        $top_score_3 + 
                        $top_score_4 + 
                        $top_score_5 + 
                        $top_score_6 + 
                        $top_score_7 + 
                        $top_score_8;
    ?>

以上选择我需要的数据,即8个值。我需要将它们变成JSON数组或其他东西才能将它用于我的图表数据:

var options = {
  type: 'line',
  data: {
    labels: ["SDO", 
             "DCTO", 
             "ED", 
             "CA", 
             "DHPT", 
             "IRT", 
             "GL", 
             "IL"       
             ],
    datasets: [
                {
                data: [5, 3, 3, 5, 2, 3, 4, 2], // these need to be my scores
                borderColor: "rgba(54, 191,191, 0.6)",
                backgroundColor : "rgba(54, 191,191, 0.6)",
                borderWidth: 1
                },
              ]
        },
  options: {
    legend: {
        display: false
     },
    scales: {
        yAxes: [{
        ticks: {
            reverse: false,
            fontSize: 15,
            beginAtZero: true,
            stepSize: 1
        }
      }]
    }

  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);    

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望以json格式从php脚本获得响应。在这种情况下,函数headerjson_encode将帮助您完成此操作

$data = [
    $top_score_1, $top_score_2, $top_score_3, $top_score_4,
    $top_score_5, $top_score_6, $top_score_7, $top_score_8
];

header('Content-Type: application/json');
echo json_encode($data);