php - 根据输入从 php 接收图表

标签 php mysql charts google-visualization

我对 php 很陌生,我试图根据输入(从下拉列表)显示一些特定记录。 从 Customer.php 页面中,我选择一个客户,该客户将向 Analysis.php 发送请求,该请求将执行数据库操作并以表格格式显示结果。现在我尝试使用数据库的结果绘制图表。

我不知道从哪里开始,任何帮助将不胜感激。

下面是我的代码。 客户.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

我想将其转换为图表,我尝试包含谷歌图表,但没有成功。

提前致谢。 :)

最佳答案

我建议您使用pChart 。这是一个免费的 PHP 库,非常易于使用,它包含 a lot of basic examples

关于php - 根据输入从 php 接收图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29627520/

相关文章:

javascript - PHP - Jquery,在每行下方显示快速详细信息 slider

php - 使用 EasyApns 添加推送通知

mysql - 在单个查询中更新和选择。 Scala 中的 Sql 语法错误

excel - 如何在vba(excel)中使用给定范围的一部分,即范围中的某些行

javascript - 使用 AJAX 将 Windows-1252 转换为 UTF-8

php - 增量停止在 10

javascript - 如何使用 DOM2 方法在 Javascript 中格式化 JSON?

php mysql查询字符串数组

c# - 以编程方式设置 Excel 图表标题

C# WPF D3(动态数据显示)图表项目