gpt4 book ai didi

php - JPGraph + mysql数组不读取值

转载 作者:行者123 更新时间:2023-11-29 13:47:49 27 4
gpt4 key购买 nike

我正在连接到 mySQL 并运行一个简单的查询,并将查询返回到一个数组中,然后内爆该数组并尝试使用 jpgraph 绘制它的图形,但我没有获得任何数据点。

<?php
// content="text/plain; charset=utf-8"
include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph.php");
include($_SERVER["DOCUMENT_ROOT"] . "/jpgraph/jpgraph_line.php");
$monday1 = strtotime("this monday");
$monday2 = date("Y-m-d",$monday1);
//connect to Database and return graph values
$con=mysql_connect("localhost:3306","root","","mysql");
$db_selected = mysql_select_db('mysql', $con);
if (!$db_selected)
{
die ('Can\'t use mysql : ' . mysql_error());
}
$mondayquery = "SELECT invoicetotal
FROM thermdata
WHERE CAST( datestamp AS DATE ) = '$monday2'
ORDER BY datehour";
$mondayresult = mysql_query($mondayquery,$con);
while($mondayinfo = mysql_fetch_array( $mondayresult ))
{
$monimp[] = $mondayinfo['invoicetotal'];
}

// Convert the array
$mondayfinal = implode(",", $monimp);

//set variables for graph values as array
$datay1 = array($mondayfinal);

// Setup the graph
$graph = new Graph(1200,600);
$graph->SetScale("intint");
$theme_class=new UniversalTheme;
$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing(false);
$graph->SetMargin(10,10,-500,10);
$graph->SetBox(false);
$graph->legend->SetPos(.01,0,'left','top');
$graph->legend->SetColumns(7);
$graph->legend->SetFont(FF_FONT2,FS_NORMAL, 72);
$graph->legend->SetHColMargin(10);
$graph->legend->SetLineWeight(5);

$graph->img->SetAntiAliasing();

$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine(true);
$graph->yaxis->HideTicks(true,true);
$graph->yaxis->SetColor('white','white');

$graph->xgrid->Show();
$graph->xgrid->SetLineStyle("solid");
$graph->xaxis->SetTickLabels(array('00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24'));
$graph->xgrid->SetColor('#E3E3E3');

// Create the first line
$p1 = new LinePlot($datay1);
$graph->Add($p1);
$p1->SetColor("#ff0000");
$p1->SetLegend('Sunday');

// Output line
$graph->Stroke();
mysql_close($con);
?>

基本上,数据库每小时都会包含一个新的发票总计,我想将它们在本周每天 24 小时内绘制成图表。

变量 $mondayfinal 解析为“0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,278,627, 1235,1919,2015”,但我不明白我做错了什么,导致 jpgraph 无法绘制它们。

任何帮助将不胜感激!

最佳答案

您需要确保 $datay1 数组的每个元素都有单独的点,即:

Array ( [0] => a [1] => b [2] => c ) 

目前,代码将创建一个单元素数组,所有值都位于元素 0 处:

Array ( [0] => a,b,c )

这在 jpgraph 中不起作用。

相反,尝试在 csv 形式的 $mondayfinal 上explode()来创建正确的数组只需使用 $monimp 来代替,因为它看起来是正确的格式。

关于php - JPGraph + mysql数组不读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17155637/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com