案例:我正在创建sql数据的按钮(while循环),对于每一行结果(machine_id,tag_name,real_value),我创建一个按钮。这个按钮有一个onclick方法,它调用javascript方法“drawChart()”,并弹出一个带有静态查询的图表。我正在尝试使用单击的按钮的值动态显示此查询。
创建按钮的一段代码。
while ($row = mysqli_fetch_array($result)) {
if ($row["int_value"] == 0 && $row["real_value"] == 0.0 && $row["bool_value"] != "") {
$data[] = $row;
$output .= '
<tr>
<td width="30%"><button name="view" value="view" id="id" class="view_btn_data" onclick="drawChart(\'' . $row["tag_name"] . '\')">' . $row["tag_name"] . '</button></td>
<td width="30%">' . $row["bool_value"] . '</td>
<td width="40%">' . $row["datetime"] . '</td>
</tr>
';
}
drawChart()的Javascript代码:
function drawChart()
{
google.charts.load('current', {'packages': ['corechart']});
google.charts.setOnLoadCallback(drawChart);
function drawChart(tagname) {
var jsonData = $.ajax({
url: "testChart.php",
dataType: "json",
data: {tagname: tagname},
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData);
var options = {
chart: {
title: 'Values',
},
width: 900,
height: 500
};
//var chart = new google.charts.Line(document.getElementById('chart_div'));
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
//chart.draw(data, google.charts.Line.convertOptions(options));
chart.draw(data, options);
$('#chartModal').modal("show");
}
}
图表获取testChart.php的数据,查询如下:
$tagname = $_REQUEST['tagname'];
$Chart = "SELECT *
FROM (
SELECT *,
@rn := if( @tag_name = tag_name,
@rn + 1,
if(@tag_name := tag_name, 1, 1)
) as tag_count
FROM waardes
CROSS JOIN ( SELECT @rn := 0, @tag_name := '') as vars
ORDER BY tag_name
) as T
WHERE tag_count < 51 AND machine_id LIKE 3 AND tag_name LIKE '. $tagname .'
ORDER BY datetime DESC";
它有效,但是使用静态图表,点是,如果单击该按钮,我想将该$行的参数传递给查询“WHERE tag_count&lt; 51 AND machine_id LIKE $的变量row [“machine_id”] AND tag_name LIKE $ row [“tag_name”]的变量“。
*对不起,如果它有点乱,不是真的很熟悉php,html和javascript。
答案 0 :(得分:0)
以下更改会将标记名链接到drawchart()函数,并将其与您的ajax请求一起发送。
然后只需读取PHP中的标记名并将其构建到MySQL查询中。
PHP
<td width="30%"><button name="view" value="view" id="id" class="view_btn_data" onclick="drawChart(\'' . $row["tag_name"] . '\')">' . $row["tag_name"] . '</button></td>
JS
function drawChart(tagname) {
var jsonData = $.ajax({
url: "testChart.php",
dataType: "json",
data{tagname:tagname}
async: false
}).responseText;