首先,提前感谢您的帮助。几天来,我一直在努力弄清楚如何将角度图对象挂接到 mysql 数据库中。我目前有使用静态数据的图表。我的项目中有一个通用的 php 文件,看起来像这样:
// Create the connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//echo "Connected successfully";
//the sql to run
$sql = "SELECT * FROM Test2";
//run it
$result = $conn->query($sql);
//check for results
if ($result->num_rows > 0) {
//create an array
$dataarray[] = array();
//loop em
while($row =mysqli_fetch_assoc($result)) {
$dataarray[] = $row;
}
//make em json
echo json_encode($dataarray);
}
//} else {
// echo "0 results";
//}
//close out our connection
$conn->close();
?>
这工作正常。此表返回几行,其中包含标题名称和值。目前只有测试数据。
在我的 html/javascript 中,我有以下内容:
<script>
angular.module("ChartApp", ["chart.js"]).controller("PieCtrl", function ($scope, $http) {
$http.get('./GetTestData.php').
success(function(data) {
$scope.records = data;
});
$scope.labels = ["Download Sales", "In-Store Sales", "Mail-Order Sales"];
$scope.data = [300, 500, 100];
});
</script>
我不确定从这里去哪里。任何帮助,将不胜感激。