使用GD和MySQL数据的PHP折线图
我正在尝试绘制一个简单的线图,它将使用数据库中的值。目前,我有一个函数可以按预期获取数据,并且我还有另一个 .php
文件,该文件应该是构建图像的文件。
我基本上使用了 此处 中的示例,如果这些值是手动输入的。然而,当我尝试调用 $arrval = functionToGetValues("$keyword");
它只是不起作用,它不会绘制任何东西。我猜这主要是因为 GD 太混乱了,因为即使使用普通的 php 命令它似乎也不能很好地工作。
我认为没有必要发布确切的代码,因为 functionToGetValues 可以工作并且它只返回一个值数组。
我还尝试使用范围,例如在 functionToGetValues()
中初始化 $arrvals
但它不起作用。我的调用顺序是:
/main.php
include "conn.php";
....
dbquery("$keyword");
//in dbquery($keyword)...
$arrval = array(...);
for($i=1; $i<30;$i++)
{
$sql = mysql_query("SELECT * FROM table WHERE data LIKE '%$keyword%'") or die("Query error: ". mysql_error());;
$total = mysql_num_rows($sql);
$arrval [$i] = $total;
}
<img src='imgdraw.php?keyword=$keyword'vspace='50'/>";
我也尝试了这种类型的解决方案但包含“conn.php”似乎不适用于 GD -_-。我找不到原因:(
I'm trying to draw a simple line graph which would use values from the database. At the moment I have a function which gets my data as it should and I also have another .php
file which should be the one where the image is constructed.
I've basically used the example from here and it works if the values are typed manually. Nevertheless when I try to call $arrval = functionToGetValues("$keyword");
it just won't work it won't draw anything. I'm guessing it's mostly because the GD
is so messed up because it doesn't seem to work too good even with normal php commands.
I think it's unnecessary to post exact code because functionToGetValues
works and it just returns an array of values.
I also tried to play with scopes like initializing the $arrvals
in functionToGetValues()
but it didn't work. My calling sequence was:
/main.php
include "conn.php";
....
dbquery("$keyword");
//in dbquery($keyword)...
$arrval = array(...);
for($i=1; $i<30;$i++)
{
$sql = mysql_query("SELECT * FROM table WHERE data LIKE '%$keyword%'") or die("Query error: ". mysql_error());;
$total = mysql_num_rows($sql);
$arrval [$i] = $total;
}
<img src='imgdraw.php?keyword=$keyword'vspace='50'/>";
I also tried this type of solution but the include "conn.php" just doesn't seem to work with GD -_-. I can't find out the reason :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果你对 GD 不太感兴趣,你可以使用谷歌的图表工具来生成图表。我一直发现它易于使用且文档齐全,并且它生成的图表看起来非常不错。
http://code.google.com/apis/chart/interactive /docs/gallery/linechart.html
您将查询数据库,然后构建一个数组,然后将其导出到带有 php 数组的 javascript 标记内
echo json_decode();
If you dont have much luck with GD You could use google's chart tools for generating graphs. I have always found it easy to use and well documented and the graphs it generates look pretty good.
http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html
You would query your database then build up an array before exporting it inside a javascript tag with the a php array with
echo json_decode();