1

I'm having to read a series of X and Y values out of a table for plotting with pChart....i know this works for single series but I've been trying to get the following code to work for several series for a while now. $pidresults are the output of a search form which are used to extract the various X and Y pairs from another table which uses that value as its foreign key.

 $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");
        }

Essentially i think my problem is in creating the variable names inside the WHILE loop (not sure correct way to generate increasing variable names), but I cant for the life of me figure out whats wrong. The $myData stuff is just for pChart, passing the arrays to the graphing script.

Any help is greatly appreciated.

6
  • 2
    Why are you suffixing the variable names to make them unique when they're arrays.... all you're doing is creating loads and loads of arrays that only contain a single entry Commented Jan 31, 2014 at 15:04
  • There will be lots of arrays. The SQL query pulls as many plots from the table as there are values in $pidresults.....i.e. the loop should be creating all the X and Y pairs where each one is an array anyway Commented Jan 31, 2014 at 15:07
  • Probably best to use an array of arrays then. Commented Jan 31, 2014 at 15:23
  • Does it matter that the arrays might all be different lengths when putting them all inside one multidimensional array though? Not all XY pairs will be the same length. Commented Jan 31, 2014 at 15:29
  • You check the docs carefully? wiki.pchart.net/doc.dataset.addpoints.html Pretty sure you can add the points at once. if you show us some how you want your chart to look like maybe it will be easier. Commented Jan 31, 2014 at 15:30

2 Answers 2

1

You should use arrays. You can then have the x's and y's as and array:

$x[0], $x[1], etc.

 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

Other ways to store these (two dimensional arrays for multiple series):

$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

For readability, you could also have $series be a string, eg 'series1' so

$x['series1'][0];
Sign up to request clarification or add additional context in comments.

2 Comments

Hi ssaltman, so I would essentially be creating arrays inside arrays?
I dont even think you need arrays inside arrays. YOu could use a two dimensional array. I'll expand my answer so the code looks nice.
0

I would be tempted to do it like this, assuming that masterID is an integer:-

$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();
}

Note, no experience of pchart so that bitis pretty much copied from you

2 Comments

Thanks Kickstart. I had a go with that but no luck. I'm wondering if its more to do with the limitations of pChart. I'm going to re-read the docs and have a play over the weekend.
Afraid I have not use pchart at all. Possible that the parameters need to be passed in a different way but would need to do some digging to figure it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.