通过获取mysql数据库表值,想要创建一个PHP条形图。我用JpGraph绘制条形图。 我想从'学生'表中获取数据。我有一个名为“category”的列,其中值为“Full time”或“Part time”。我想算不上。类别为“全时”的行和其中类别为“兼职”的行数。 x轴应为类别,y轴应为相关行数。 PHP代码如下;
<?php
require_once('/jpgraph-3.5.0b1/src/jpgraph.php');
require_once('/jpgraph-3.5.0b1/src/jpgraph_bar.php');
$db = mysql_connect("localhost", "root","") or die(mysql_error());
mysql_select_db("mphildatabase",$db) or die(mysql_error());
$sql1 = mysql_query("SELECT category FROM student") or die(mysql_error());
$sql2 = mysql_query("SELECT COUNT(*) FROM student WHERE category='Full Time'") or die(mysql_error());
$sql3 = mysql_query("SELECT COUNT(*) FROM student WHERE category='Part Time'") or die(mysql_error());
while($row1 = mysql_fetch_array($sql1))
{
$row2 = mysql_num_rows($sql2);
$row3 = mysql_num_rows($sql3);
$data[] = $row2;
//$data[] = $row3;
$leg[] = $row1['category'];
}
$graph = new Graph(950,750,"auto");
$graph->SetScale("textint");
$graph->img->SetMargin(50,30,50,50);
$graph->SetShadow();
$graph->xaxis->SetTickLabels($leg);
$bplot = new BarPlot($data);
$bplot->SetFillColor("lightgreen");
$bplot->value->Show();
$bplot->value->SetFont(FF_ARIAL,FS_BOLD);
$bplot->value->SetAngle(45);
$bplot->value->SetColor("black","navy");
$graph->Add($bplot);
$graph->Stroke(); ?>
这将给出每行的单独条形图。每行都会打印条形图,所有这些条形图的y轴值将为1.如何绘制仅有2个条形图的条形图,其中一个显示类别为“全时”的行数和其他条形图显示类别为“兼职”的行数 谢谢!
答案 0 :(得分:1)
您必须只使用一个查询,并在一个循环中填充数组。 您的查询必须是:
"SELECT count(*) as num_rows, category from
student WHERE category ='Full Time' or Category= 'Part Time'
Group by Category
"
...
while($row1 = mysql_fetch_array($sql1))
{
$leg[] = $row1['category'];
$data[] = $row1['num_rows']
}