【发布时间】:2020-10-07 19:28:22
【问题描述】:
#我已经构建了一个 php 应用程序来查询数据库并从特定行返回结果。我现在想将这些结果发布到生成图形的 javascript 中。我需要帮助,因为我是 php 和 javascript 的新手。这是我在同一个文件中的两个源代码。
...
<?php
$servername = "#mysever";
$username = "#myusername";
$password = "#mypassword";
$dbname = "#mysqldbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Zone, Premises, ServiceOrders, Balance FROM wrzones WHERE Zone='WR Zone 8'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>Zone</th><th>Premises</th><th>ServiceOrders</th><th>Balance</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["Zone"]."</td><td>".$row["Premises"]." </td><td>".$row["ServiceOrders"]." </td><td>".$row["Balance"]." </td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
...
#查询产生以下输出:Zone = WR Zone 1;房地 = 130;服务订单 = 60;余额 = 70;
#Below 是通过 canvasjs.com 提供的 javascript 的开始 ...
<script>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
title:{
text: "Zone 1 - Fiber Sign-ups", #I want Zone 1 to come from .php output from "Zones"
horizontalAlign: "center"
},
data: [{
type: "doughnut",
startAngle: 130, #I want 130 to come from .php output from "Premises"
//innerRadius: 130, #I want 130 to come from .php output from "Premises"
indexLabelFontSize: 17,
indexLabel: "{label} - #percent%",
toolTipContent: "<b>{label}:</b> {y} (#percent%)",
dataPoints: [
{ y: 60, label: "Sign-ups" },
{ y: 70, label: "Balance" }
... #我希望 60 来自“ServiceOrders”的 .php 输出 #我希望 70 来自“Balance”的 .php 输出 ...
]
}]
});
chart.render();
}
</script>
... #HTML 输出图表 ...
<div>
<h2 align="center">City Sites</h1>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<script src="canvasjs.min.js">
</script>
</div>
</div>
#我尝试将数据添加到javascript中
【问题讨论】:
标签: javascript php sql