我必须从表格中读取一系列X和Y值以便用pChart进行绘图....我知道这适用于单个系列但我一直试图让以下代码适用于几个系列一段时间了。 $ pidresults是搜索表单的输出,用于从另一个表中提取各种X和Y对,该表使用该值作为其外键。
$pidresults = $_SESSION['pidresults'];
$pidsize = count($pidresults);
// select plottable data from sampleSlave table
for ($i=0; $i<$pidsize; $i++) {
$val = $pidresults[$i];
$sql="SELECT * FROM sampleSlave WHERE masterID=$val AND xaxisdata>=1 ORDER BY xaxisdata";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
while ($row = mysql_fetch_array($result)) {
$varnameX = "xData$val";
$varnameY = "yData$val";
$$varnameX[] = $row['xaxisdata'];
$$varnameY[] = $row['yaxisdata'];
}
$myData->addPoints($varnameX,"XPID$val");
$myData->addPoints($varnameY,"YPID$val");
$myData->setSerieOnAxis("YPID$val",1);
$myData->setScatterSerie("XPID$val","YPID$val",$i+1);
$myData->setScatterSerieDescription($val,"Sample$val");
}
基本上我认为我的问题是在WHILE循环中创建变量名称(不确定生成增量变量名称的正确方法),但我不能为我的生活弄清楚什么是错的。 $ myData的东西只适用于pChart,将数组传递给图形脚本。
非常感谢任何帮助。
答案 0 :(得分:1)
你应该使用数组。然后你可以将x和y作为数组:
$ x [0],$ x [1]等等。
for ($i=0; $i<=10; $i++)
{
$x[$i] = blah blah blah //assigns the next incremental value of array $x
}
http://www.w3schools.com/php/php_arrays.asp
存储这些的其他方法(多个系列的二维数组):
$x[$series][$i] //$x[0][0] is the first x point in the first series
$y[$series][$i] //$y[0][0] is the first y point in the first series
为了便于阅读,你也可以让$ series成为一个字符串,例如'series1'所以
$x['series1'][0];
答案 1 :(得分:0)
我很想这样做,假设masterID是一个整数: -
$pidresults = $_SESSION['pidresults'];
// select plottable data from sampleSlave table
$sql = "SELECT masterID, xaxisdata, yaxisdata
FROM sampleSlave
WHERE masterID IN = ".implode(',', $pidresults)."
AND xaxisdata >= 1
ORDER BY FIELD (masterID, ".implode(',', $pidresults)."), xaxisdata";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
$varnameX = array();
$varnameY = array();
$prevMasterID = 0;
$i = 1;
while ($row = mysql_fetch_array($result))
{
if ($prevMasterID != $row['masterID'])
{
if ($prevMasterID != 0)
{
$myData->addPoints($varnameX,"XPID$prevMasterID");
$myData->addPoints($varnameY,"YPID$prevMasterID");
$myData->setSerieOnAxis("YPID$prevMasterID",1);
$myData->setScatterSerie("XPID$prevMasterID","YPID$prevMasterID",$i++);
$myData->setScatterSerieDescription($prevMasterID,"Sample$prevMasterID");
$varnameX = array();
$varnameY = array();
}
$prevMasterID = $row['masterID'];
}
$varnameX[] = $row['xaxisdata'];
$varnameY[] = $row['yaxisdata'];
}
if ($prevMasterID != 0)
{
$myData->addPoints($varnameX,"XPID$prevMasterID");
$myData->addPoints($varnameY,"YPID$prevMasterID");
$myData->setSerieOnAxis("YPID$prevMasterID",1);
$myData->setScatterSerie("XPID$prevMasterID","YPID$prevMasterID",$i+1);
$myData->setScatterSerieDescription($prevMasterID,"Sample$prevMasterID");
$varnameX = array();
$varnameY = array();
}
请注意,没有pchart的经验,因此很少有人从你那里复制了