Php 如何从字段名和列中检索数据以生成图形
下面的代码是我用来尝试使用pchart生成图形的代码。我已经能够生成一些图形。目前的图表是这样的:第二张图表应该是基于我手动输入的值。我想做的是从mysql数据库中检索数据,而不需要手动输入数据,因此图表是动态生成的 这是我的密码。我不确定是查询还是不正确的数组导致图表无法生成响应Php 如何从字段名和列中检索数据以生成图形,php,mysql,Php,Mysql,下面的代码是我用来尝试使用pchart生成图形的代码。我已经能够生成一些图形。目前的图表是这样的:第二张图表应该是基于我手动输入的值。我想做的是从mysql数据库中检索数据,而不需要手动输入数据,因此图表是动态生成的 这是我的密码。我不确定是查询还是不正确的数组导致图表无法生成响应 <?php /* CAT:Bar Chart */ /* pChart library inclusions */ include("pchart/class/pData.class.php");
<?php
/* CAT:Bar Chart */
/* pChart library inclusions */
include("pchart/class/pData.class.php");
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");
/* Create and populate the pData object */
$myData = new pData();
$myData->addPoints(array($Yes,$Result));
$myData->addPoints(array($No,$Result));
$myData->addPoints(array($Undecided,$Result));
$myData->setAxisName(0,"Number of Responses");
$myData->addPoints(array("Yes","No","Undecided"),"Types of Responses");
$myData->setSerieDescription("Types of Responses","Types of Responses");
$myData->setAbscissa("Types of Responses");
$myData->setAbscissaName("Types of Responses");
/* Connect to the MySQL database */
$db = mysql_connect("host", "user", "pass");
mysql_select_db("thedatabase",$db);
/* Build the query that will returns the data to graph */
$Requete = "SELECT `Do you have an interest in Green IT` FROM `replies`";
$Result = mysql_query($Requete,$db);
$Yes=""; $No=""; $Undecided="";
while($row = mysql_fetch_array($Result))
{
/* Push the results of the query in an array */
$Yes[] = $row["Yes"];
$No[] = $row["No"];
$Undecided[] = $row["Undecided"];
}
/* Create the pChart object */
$myPicture = new pImage(500,500,$myData);
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
$myPicture->setFontProperties(array("FontName"=>"pchart/fonts/pf_arma_five.ttf","FontSize"=>6));
/* Draw the chart scale */
$myPicture->setGraphArea(100,30,480,480);
$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM));
/* Turn on shadow computing */
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
/* Create the per bar palette */
$Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
"1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100),
"2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100),
"3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100),
"4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100),
"5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100),
"6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100),
"7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100));
/* Draw the chart */
$myPicture->drawBarChart(array("DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"Rounded"=>TRUE,"Surrounding"=>30,"OverrideColors"=>$Palette));
/* Write the legend */
$myPicture->drawLegend(570,215,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
/* Render the picture (choose the best way) */
$myPicture->autoOutput("pictures/example.drawBarChart.palette.png");
?>
我哪里做错了
谢谢假设每个受访者都有一条记录,那么查询应该是这样的-
SELECT `Do you have an interest in Green IT`, COUNT(*) AS num
FROM replies
GROUP BY `Do you have an interest in Green IT`
然后,此查询返回的每一行都可以作为点添加到数据集中。正如其他人指出的,为了将数据传递给图表类,您需要首先运行查询-
<?php
/* CAT:Bar Chart */
/* pChart library inclusions */
include("pchart/class/pData.class.php");
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");
/* Create and populate the pData object */
$myData = new pData();
$myData->setAxisName(0,"Number of Responses");
$myData->setSerieDescription("Types of Responses","Types of Responses");
$myData->setAbscissa("Types of Responses");
$myData->setAbscissaName("Types of Responses");
/* Connect to the MySQL database */
$db = mysql_connect("host", "user", "pass");
mysql_select_db("thedatabase",$db);
/* Build the query that will returns the data to graph */
$Requete = "SELECT `Do you have an interest in Green IT` AS resp, COUNT(*) AS num FROM replies GROUP BY resp";
$Result = mysql_query($Requete,$db);
while($row = mysql_fetch_array($Result))
{
$myData->addPoints(array($row['resp'], $row['num']));
}
您试图在从数据库中取出数据之前将数据传递给addPoints方法。看起来你也在使用数据,有点奇怪。您正在使用行中的潜在值作为列名。您需要使用@nnichols post之类的工具
在addPoints
方法中,您试图传递什么作为第二个选项?您传递的是数据库资源,但我发现奇怪的是,图表应用程序在要求特定的数字时需要它-是您想要传递的行总数吗
我还建议您重命名表中的列,使用像对绿色感兴趣\u it
这样的名称,而不是您对绿色it感兴趣吗
就使用前获取数据而言,您需要以这种方式使用它:
<?php
/* CAT:Bar Chart */
/* pChart library inclusions */
include("pchart/class/pData.class.php");
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");
/* Connect to the MySQL database */
$db = mysql_connect("host", "user", "pass");
mysql_select_db("thedatabase",$db);
/* Build the query that will returns the data to graph */
$Yes = "";
$No = "";
$Undecided = "";
$Requete = "SELECT `Do you have an interest in Green IT` FROM `replies`";
$Result = mysql_query($Requete,$db);
while($row = mysql_fetch_array($Result))
{
/* Push the results of the query in an array */
$Yes[] = $row["Yes"];
$No[] = $row["No"];
$Undecided[] = $row["Undecided"];
}
/* Create and populate the pData object */
$myData = new pData();
$myData->addPoints(array($Yes,$Result));
$myData->addPoints(array($No,$Result));
$myData->addPoints(array($Undecided,$Result));
$myData->setAxisName(0,"Number of Responses");
$myData->addPoints(array("Yes","No","Undecided"),"Types of Responses");
$myData->setSerieDescription("Types of Responses","Types of Responses");
$myData->setAbscissa("Types of Responses");
$myData->setAbscissaName("Types of Responses");
/* Create the pChart object */
$myPicture = new pImage(500,500,$myData);
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
$myPicture->setFontProperties(array("FontName"=>"pchart/fonts/pf_arma_five.ttf","FontSize"=>6));
/* Draw the chart scale */
$myPicture->setGraphArea(100,30,480,480);
$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM));
/* Turn on shadow computing */
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
/* Create the per bar palette */
$Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
"1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100),
"2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100),
"3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100),
"4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100),
"5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100),
"6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100),
"7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100));
/* Draw the chart */
$myPicture->drawBarChart(array("DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"Rounded"=>TRUE,"Surrounding"=>30,"OverrideColors"=>$Palette));
/* Write the legend */
$myPicture->drawLegend(570,215,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
/* Render the picture (choose the best way) */
$myPicture->autoOutput("pictures/example.drawBarChart.palette.png");
?>
在填充数据之前,您正在使用$result、$yes$no和$defined变量。我应该将查询移到生成的图形之前?表的结构是什么?发布CREATETABLE语句。正如@Bengrifiths所指出的,您确实应该对字段名做些什么。使用句子真是个糟糕的主意。非常感谢你们的帮助。但我遇到了一个问题——数据被跨条多次复制。我试图将其限制为3条,但很难做到这一点,因为我只想输出字段“您对绿色it感兴趣吗”的图形,而不是其他任何内容。它确实有效,但是,很小的一点是,我不能让它为每个响应显示单个条。是(19)否(6)未决定(3),而不是我现在拥有的众多酒吧。感谢您在PMA或任何您喜欢的db工具中运行查询来检查您的查询。如果添加了GROUPBY子句,则每个不同的响应只能有一条记录。
<?php
/* CAT:Bar Chart */
/* pChart library inclusions */
include("pchart/class/pData.class.php");
include("pchart/class/pDraw.class.php");
include("pchart/class/pImage.class.php");
/* Connect to the MySQL database */
$db = mysql_connect("host", "user", "pass");
mysql_select_db("thedatabase",$db);
/* Build the query that will returns the data to graph */
$Yes = "";
$No = "";
$Undecided = "";
$Requete = "SELECT `Do you have an interest in Green IT` FROM `replies`";
$Result = mysql_query($Requete,$db);
while($row = mysql_fetch_array($Result))
{
/* Push the results of the query in an array */
$Yes[] = $row["Yes"];
$No[] = $row["No"];
$Undecided[] = $row["Undecided"];
}
/* Create and populate the pData object */
$myData = new pData();
$myData->addPoints(array($Yes,$Result));
$myData->addPoints(array($No,$Result));
$myData->addPoints(array($Undecided,$Result));
$myData->setAxisName(0,"Number of Responses");
$myData->addPoints(array("Yes","No","Undecided"),"Types of Responses");
$myData->setSerieDescription("Types of Responses","Types of Responses");
$myData->setAbscissa("Types of Responses");
$myData->setAbscissaName("Types of Responses");
/* Create the pChart object */
$myPicture = new pImage(500,500,$myData);
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
$myPicture->drawGradientArea(0,0,500,500,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
$myPicture->setFontProperties(array("FontName"=>"pchart/fonts/pf_arma_five.ttf","FontSize"=>6));
/* Draw the chart scale */
$myPicture->setGraphArea(100,30,480,480);
$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM));
/* Turn on shadow computing */
$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
/* Create the per bar palette */
$Palette = array("0"=>array("R"=>188,"G"=>224,"B"=>46,"Alpha"=>100),
"1"=>array("R"=>224,"G"=>100,"B"=>46,"Alpha"=>100),
"2"=>array("R"=>224,"G"=>214,"B"=>46,"Alpha"=>100),
"3"=>array("R"=>46,"G"=>151,"B"=>224,"Alpha"=>100),
"4"=>array("R"=>176,"G"=>46,"B"=>224,"Alpha"=>100),
"5"=>array("R"=>224,"G"=>46,"B"=>117,"Alpha"=>100),
"6"=>array("R"=>92,"G"=>224,"B"=>46,"Alpha"=>100),
"7"=>array("R"=>224,"G"=>176,"B"=>46,"Alpha"=>100));
/* Draw the chart */
$myPicture->drawBarChart(array("DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"Rounded"=>TRUE,"Surrounding"=>30,"OverrideColors"=>$Palette));
/* Write the legend */
$myPicture->drawLegend(570,215,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
/* Render the picture (choose the best way) */
$myPicture->autoOutput("pictures/example.drawBarChart.palette.png");
?>