PHP-一页中的多个Ajax请求如何进行最佳实践
作者:互联网
我目前正在研究Google图表,并且已经有了一个基本的设置.
它当前所做的是连接到DB,并基于1个查询返回一个数据集.我想知道的是,如果我想向数据库绘制更多具有不同查询的图表,该怎么做?或最佳做法是什么?
例如,一个查询已经存在一个连接,如何添加另一个查询,然后根据返回的内容绘制图表?
我知道这可能是一个广泛的问题,但是也许有人可以告诉我如何从数据库返回不同的查询/数据集?
这是我的代码:
$(document).ready(function(){
console.log("hello world")
//alert("result")
$.ajax({
url:"data.php",
dataType : "json",
success : function(result) {
google.charts.load('current', {
'packages':['corechart','bar']
});
google.charts.setOnLoadCallback(function() {
console.log(result[0]["name"])
drawChart(result);
});
}
});
//add a 2nd call - will need a 2nd draw charts to draw the different dataset assuming it will be different
// - will need a 2nd data.php as the query will be different on the dataset
$.ajax({
url:"data.php",
dataType : "json",
success : function(result2) {
google.charts.load('current', {
'packages':['corechart','bar']
});
google.charts.setOnLoadCallback(function() {
console.log(result2[0]["name"])
drawChart(result2);
});
}
});
function drawChart(result) {
var data = new google.visualization.DataTable();
data.addColumn('string','Name');
data.addColumn('number','Quantity');
var dataArray=[];
$.each(result, function(i, obj) {
dataArray.push([ obj.name, parseInt(obj.quantity) ]);
});
data.addRows(dataArray);
var piechart_options = {
title : 'Pie Chart: How Much Products Sold By Last Night',
width : 400,
height : 300
}
var piechart = new google.visualization.PieChart(document
.getElementById('piechart_div'));
piechart.draw(data, piechart_options)
var columnchart_options = {
title : 'Bar Chart: How Much Products Sold By Last Night',
width : 400,
height : 300,
legend : 'none'
}
//var barchart = new google.visualization.BarChart(document
// .getElementById('barchart_div'));
//barchart.draw(data, barchart_options)
var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
chart.draw(data, google.charts.Bar.convertOptions(columnchart_options));
} //have added this column chart but need to wrok out if it is best practice????
});
我从数据库查询中获取了一个对象,但是我想知道如何从同一数据库连接返回更多/不同的数据集?例如,如果我想用该查询返回的数据集绘制另一个图表,请从产品中选择*,其中name =“ Product1”或name =“ Product2”;
0: Object { id: "1", name: "Product1", quantity: "2" }
1: Object { id: "2", name: "Product2", quantity: "3" }
2: Object { id: "3", name: "Product3", quantity: "4" }
3: Object { id: "4", name: "Product4", quantity: "2" }
4: Object { id: "5", name: "Product5", quantity: "6" }
5: Object { id: "6", name: "Product6", quantity: "11" }
对于我的PHP代码而言,如下所示:
data.php
<?php
require_once 'database.php';
$stmt = $conn->prepare('select * from product');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
echo json_encode($results);
?>
为database.php
<?php
$conn = new PDO('mysql:host=192.168.99.100;dbname=demo','root', 'root');
?>
注意:可能是this
解决方法:
每次加载页面时,Google图表只需加载一次,
并非每次您都需要绘制图表时
另外,也可以使用google.charts.load代替-> $(文件).就绪
在执行回调/诺言之前,它将等待页面加载
推荐类似于以下代码段的设置…
google.charts.load('current', {
packages: ['corechart', 'bar']
}).then(function () {
$.ajax({
url: 'data.php',
dataType: 'json'
}).done(drawChart1);
$.ajax({
url: 'data.php',
dataType: 'json'
}).done(drawChart2);
});
function drawChart1(result) {
...
}
function drawChart2(result) {
...
}
标签:php,ajax,google-visualization 来源: https://codeday.me/bug/20191014/1912544.html