0

JSON/AJAX を介して MySQL データを Google Charts に渡す作業はほぼ完了していると思います。JSON 文字列を正しい形式で出力できますが、SQL データを出力していません。私はどこでも解決策を探しましたが、結果はありませんでした。コードに何が欠けているか、誰にもわかりますか?

JSON 出力

{"cols":[{"id":"","label":"projid","type":"string"},{"id":"","label":"hours","type":"number"}],"rows":[{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]}]}

PHP->JSON

<?php
// -----> Query MySQL and parse into JSON below. <------

// write your SQL query here (you may use parameters from $_GET or $_POST if you need them)

require_once ("Includes/session.php");
require_once ("Includes/simplecms-config.php");
require_once ("Includes/connectDB.php");

$recId = null;
$projid = null;
$hours = null;

        $recId = $_GET['id'];
        $projid = $_GET['projid'];
        $hours = $_GET['hours'];
        $query = "SELECT projid, hours FROM hours WHERE id = ?";
        $statement = $databaseConnection->prepare($query);
        $statement->bind_param('d', $recId);
        $statement->execute();
        $results = $statement->get_result();

  $rows = array();
  $table = array();
  $table['cols'] = array(
    array('id' => "",'label' => 'projid', 'type' => 'string'),
    array('id' => "",'label' => 'hours', 'type' => 'number')
);


    /* Extract the information from $result */
    while ($r = $results->fetch_assoc()) {
      $temp = array();

      // The following line will be used to slice the Pie chart
      $temp[] = array('v' => (string) $r['projid']); 

      // Values of each slice
      $temp[] = array('v' => (int) $r['hours']); 
      $rows[] = array('c' => $temp);
    }

$table['rows'] = $rows;

// convert data into JSON format
$jsonTable = json_encode($table);
echo $jsonTable;

?>

4

2 に答える 2

3

次のコードは、Google チャートの正しい配列を返しました。Google チャート - JSON データ

<?php 

// -----> Query MySQL and parse into JSON below. <------

require_once  ("Includes/connectDB.php");

$result = $databaseConnection->query("SELECT projid, hours FROM alloc_hours");      
    $table = array();
    $table['cols'] = array(
    array('id' => "", 'label' => 'projid', 'pattern' => "", 'type' => 'string'),
    array('id' => "", 'label' => 'hours', 'pattern' => "", 'type' => 'number')
    );
    $rows = array();
    while ($nt = $result->fetch_assoc())
    {
    $temp = array();
    $temp[] = array('v' => $nt['projid'], 'f' =>NULL);
    $temp[] = array('v' => $nt['hours'], 'f' =>NULL);
    $rows[] = array('c' => $temp);
    }
    $table['rows'] = $rows;
    $jsonTable = json_encode($table);
    echo $jsonTable;
?>

配列

{"cols":[{"id":"","label":"projid","pattern":"","type":"string"},{"id":"","label":"hours","pattern":"","type":"number"}],"rows":[{"c":[{"v":"2","f":null},{"v":"8","f":null}]},{"c":[{"v":"1","f":null},{"v":"6","f":null}]},{"c":[{"v":"3","f":null},{"v":"20","f":null}]},{"c":[{"v":"2","f":null},{"v":"10","f":null}]},{"c":[{"v":"4","f":null},{"v":"5","f":null}]},{"c":[{"v":"1","f":null},{"v":"30","f":null}]}]}
于 2013-08-08T13:49:34.217 に答える