我试图弄清楚如何将我使用PHP提取的SQL数据解析为javascript文件。
我需要将PHP数组存储到java中,因为我有一个使用javascript数组填充数据的图形。
现在我有一个刷新我的图表数据的按钮,当点击它时,它会调用我的主HTML文件中包含的这个javascript:
button.js
$(function(){
$('#refreshchart').click(function() {
var chart = $('#chart').highcharts();
chart.series[0].setData(dataarray);
});
});
Java数组" dataarray"需要从SQL数据库中提取。
我有一个PHP脚本,可以从数据库中提取所需的数据。
这是我的PHP脚本:
query.php
<?php
include 'dbcon.php';
$query0 = "SELECT count(*) FROM test WHERE ans=0";
$query1 = "SELECT count(*) FROM test WHERE ans=1";
$query2 = "SELECT count(*) FROM test WHERE ans=2";
$query3 = "SELECT count(*) FROM test WHERE ans=3";
$query4 = "SELECT count(*) FROM test WHERE ans=4";
$query5 = "SELECT count(*) FROM test WHERE ans=5";
$query6 = "SELECT count(*) FROM test WHERE ans=6";
$query7 = "SELECT count(*) FROM test WHERE ans=7";
$query8 = "SELECT count(*) FROM test WHERE ans=8";
$query9 = "SELECT count(*) FROM test WHERE ans=9";
$result0 = $mysqli->query($query0) or die($mysqli->error.__LINE__);
$result1 = $mysqli->query($query1) or die($mysqli->error.__LINE__);
$result2 = $mysqli->query($query2) or die($mysqli->error.__LINE__);
$result3 = $mysqli->query($query3) or die($mysqli->error.__LINE__);
$result4 = $mysqli->query($query4) or die($mysqli->error.__LINE__);
$result5 = $mysqli->query($query5) or die($mysqli->error.__LINE__);
$result6 = $mysqli->query($query6) or die($mysqli->error.__LINE__);
$result7 = $mysqli->query($query7) or die($mysqli->error.__LINE__);
$result8 = $mysqli->query($query8) or die($mysqli->error.__LINE__);
$result9 = $mysqli->query($query9) or die($mysqli->error.__LINE__);
$row0 = $result0->fetch_row();
$row1 = $result1->fetch_row();
$row2 = $result2->fetch_row();
$row3 = $result3->fetch_row();
$row4 = $result4->fetch_row();
$row5 = $result5->fetch_row();
$row6 = $result6->fetch_row();
$row7 = $result7->fetch_row();
$row8 = $result8->fetch_row();
$row9 = $result9->fetch_row();
echo "Number of people that chose A: ", $row1[0];
echo "<br>Number of people that chose B: ", $row2[0];
echo "<br>Number of people that chose C: ", $row3[0];
echo "<br>Number of people that chose D: ", $row4[0];
echo "<br>Number of people that chose E: ", $row5[0];
echo "<br>Number of people that chose F: ", $row6[0];
echo "<br>Number of people that chose G: ", $row7[0];
echo "<br>Number of people that chose H: ", $row8[0];
echo "<br>Number of people that chose I: ", $row9[0];
echo "<br>Number of people that chose J: ", $row0[0];
$array = array($row1[0],$row2[0],$row3[0],$row4[0],$row5[0],$row6[0],$row7[0],$row8[0],$row9[0],$row0[0]);
echo json_encode($array);
?>
现在我知道我将不得不使用json对PHP数组进行编码,以便正确格式化,并且我可能必须使用AJAX来调用PHP脚本来获取数据。
这是我遇到困难的地方,我不知道如何获取结果并将它们解析为java数组,以便可以使用SQL数据库中的结果更新图形。
非常感谢任何帮助!
谢谢, 克里斯托弗
答案 0 :(得分:1)
如果你想通过ajax调用你的php代码试试:
$(function(){
var chart = $('#chart').highcharts();
$('#refreshchart').click(function() {
$.ajax({
type: "GET",
url: "some.php",
success: function(data) {
chart.series[0].setData($.parseJSON(data));
}
});
});
});
只需将“some.php”设置为php-filename。
答案 1 :(得分:0)
此代码正常运行
var str_array=[];
function something()
{
str_array= <?php echo json_encode($array); ?>;
alert(str_array);`
}
答案 2 :(得分:0)
<script>
var contants = <?php echo json_encode($array); ?>
</script>
然后将其转换为数组。 jQuery&#39; $.parseJSON(string)
可能有所帮助。
答案 3 :(得分:0)
如果可能,请避免在代码中编写动态JavaScript,为此使用webservices,您可以使用jQuery通过AJAX调用获取此数据。
$.ajax({
url: "query.php",
data: { json: "on" },
}).done(function(data) {
console.log(data);
alert("found " + data.length + " elements" + "\n" + "first one: " + data[0].name + " = " + data[0].total);
});
我更喜欢这样写:
<?php
include 'dbcon.php';
$query =
"SELECT 'A' AS name,count(*) AS total FROM test WHERE ans=0 UNION " .
"SELECT 'B' AS name,count(*) AS total FROM test WHERE ans=1 UNION " .
"SELECT 'C' AS name,count(*) AS total FROM test WHERE ans=2 UNION " .
"SELECT 'D' AS name,count(*) AS total FROM test WHERE ans=3 UNION " .
"SELECT 'E' AS name,count(*) AS total FROM test WHERE ans=4 UNION " .
"SELECT 'F' AS name,count(*) AS total FROM test WHERE ans=5 UNION " .
"SELECT 'G' AS name,count(*) AS total FROM test WHERE ans=6 UNION " .
"SELECT 'H' AS name,count(*) AS total FROM test WHERE ans=7 UNION " .
"SELECT 'I' AS name,count(*) AS total FROM test WHERE ans=8 UNION " .
"SELECT 'J' AS name,count(*) AS total FROM test WHERE ans=9";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$rows = $result->fetch_all(MYSQLI_ASSOC);
$print_json = !empty($_GET['json']) && $_GET['json']=='on'; // your content negotiation logic here
if ($print_json) {
header('Content-Type: application/json');
echo json_encode($rows);
} else {
foreach ($rows as $row) {
echo "Number of people that chose {$row['name']}: {$row['total']}";
}
}
这样您可以获得更多控制权和更清晰的代码。我认为你的代码改进了SQL。