根据输入从 php 接收图表
Receive a graph from php based on the input
我是 php 的新手,我正在尝试根据输入(来自下拉列表)显示一些特定记录。
从 Customer.php 页面我选择了一个客户,该客户将向 analysis.php 发送请求,后者将执行数据库操作并以表格格式显示结果。现在我正在尝试使用 DB 的结果绘制图表。
我不知道从哪里开始,任何帮助将不胜感激。
下面是我的代码。
customer.php
<html>
<head>
<title>Customer</title>
<script type="text/javascript">
function validateBox(){
var customer_name = document.getElementById('customer_name').value;
if(customer_name.length == 0){
alert("Please select customer name");
return false;
//handle validation response here
} else {
//document.getElementById("form").submit();//submit form, or whatever the button is supposed to do...
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","analysis.php?q="+customer_name,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form class="form" id="form" method="post">
<table align="center">
<tr>
<td>
<label>Customer Name :</label></td>
<td><select name="customer_name" id="customer_name" required>
<option value="">Select</option>
<option value="Dell">Dell</option>
<option value="HP">HP</option>
<option value="Lenovo">Lenovo</option>
<option value="Compaq">Compaq</option>
</select></td></tr>
<td><input onclick="validateBox()" type="button" name="addrecord" value="Begin Analysis"></td></table>
</form>
</body>
</html>
和analysis.php看起来像
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db", $connection);
$q = $_GET['q'];
$resource=array();
$z=0;
$SQL = mysql_query("select resources from users ", $connection);
while ($db_field = mysql_fetch_assoc($SQL)) {
$a = $db_field['resources'];
$resource[$z] = $a;
$z++;
}
$resourcelength = count($resource);
?>
<table border = "2" width = "30%" align='center'>
<tr align ="center">
<th >resource</th>
<th ># Count</th>
<?php
for($x = 0; $x < $resourcelength; $x++) {
print("<tr>");
print("<td align = 'center'>$resource[$x]</td>");
$y=$resource[$x];
$SQL = mysql_query("SELECT count(distinct(ticket_id)) as total FROM tickets WHERE resource_name='".$y."' ", $connection);
while ($db_field = mysql_fetch_assoc($SQL)) {
$a = $db_field['total'];
print("<td align = 'center'>$a</td>");
}
print("</tr>");
}
?>
</table>
输出会像
Resource Total
A 30
B 12
C 15
D 0
X 13
我想将其转换为图表,我尝试包含 google 个图表,但没有成功。
提前致谢。 :)
我建议您使用 pChart. It's a free PHP library, really easy to use and it contains a lot of basic examples。
我是 php 的新手,我正在尝试根据输入(来自下拉列表)显示一些特定记录。 从 Customer.php 页面我选择了一个客户,该客户将向 analysis.php 发送请求,后者将执行数据库操作并以表格格式显示结果。现在我正在尝试使用 DB 的结果绘制图表。
我不知道从哪里开始,任何帮助将不胜感激。
下面是我的代码。 customer.php
<html>
<head>
<title>Customer</title>
<script type="text/javascript">
function validateBox(){
var customer_name = document.getElementById('customer_name').value;
if(customer_name.length == 0){
alert("Please select customer name");
return false;
//handle validation response here
} else {
//document.getElementById("form").submit();//submit form, or whatever the button is supposed to do...
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","analysis.php?q="+customer_name,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form class="form" id="form" method="post">
<table align="center">
<tr>
<td>
<label>Customer Name :</label></td>
<td><select name="customer_name" id="customer_name" required>
<option value="">Select</option>
<option value="Dell">Dell</option>
<option value="HP">HP</option>
<option value="Lenovo">Lenovo</option>
<option value="Compaq">Compaq</option>
</select></td></tr>
<td><input onclick="validateBox()" type="button" name="addrecord" value="Begin Analysis"></td></table>
</form>
</body>
</html>
和analysis.php看起来像
<?php
$connection = mysql_connect("localhost", "root", "");
$db = mysql_select_db("db", $connection);
$q = $_GET['q'];
$resource=array();
$z=0;
$SQL = mysql_query("select resources from users ", $connection);
while ($db_field = mysql_fetch_assoc($SQL)) {
$a = $db_field['resources'];
$resource[$z] = $a;
$z++;
}
$resourcelength = count($resource);
?>
<table border = "2" width = "30%" align='center'>
<tr align ="center">
<th >resource</th>
<th ># Count</th>
<?php
for($x = 0; $x < $resourcelength; $x++) {
print("<tr>");
print("<td align = 'center'>$resource[$x]</td>");
$y=$resource[$x];
$SQL = mysql_query("SELECT count(distinct(ticket_id)) as total FROM tickets WHERE resource_name='".$y."' ", $connection);
while ($db_field = mysql_fetch_assoc($SQL)) {
$a = $db_field['total'];
print("<td align = 'center'>$a</td>");
}
print("</tr>");
}
?>
</table>
输出会像
Resource Total
A 30
B 12
C 15
D 0
X 13
我想将其转换为图表,我尝试包含 google 个图表,但没有成功。
提前致谢。 :)
我建议您使用 pChart. It's a free PHP library, really easy to use and it contains a lot of basic examples。