phparraysjpgraph

PHP passing array created in for loop to jpgraph function


I have 3 arrays created in the following way:

$start=2013 ;
$end=2015;

$no_of_bars=$to-$from+1;

$xdata=array();
for($year=$start;$year<=$end;$year++){

     ${"y".$year}=array();
    $i=0;
    $query=mysql_query("SELECT tld_master.location,tld_dose.year, AVG(tld_dose.dose)*4 as avgdose from tld_master left join tld_dose on tld_master.tldno=tld_dose.tldno where tld_master.site='F' and tld_dose.year=$year GROUP BY tld_dose.year, tld_dose.tldno");

    while($result=mysql_fetch_array($query)){

$xdata[$i]=$result['location'];
 ${"y".$year}[$i]=$result['avgdose'];


$i++;
}

    }

It creates three arrays y2013,y2014,y2015.

I have to pass this array to jpgrpah to plot a groupbar. New Bra objects are created in this way

$j=0;
for($year=$start;$year<=$end;$year++, $j++){
    ${"plot".$year}=new BarPlot(${"y".$year});

    ${"plot".$year}->SetFillColor($color_array[$j]);

if($year!=$end){$sep=",";}else{$sep="";}
    $plots.="$"."plot".$year.$sep;
    }

There are three bar plots plot2013,plot2014,plot2015. All these three are arrays. I want to pass these arrays to jpgraph function given below:

$gbplot = new GroupBarPlot($plots);

but this is not working. But if I am changing it to the one given below, it works

$gbplot = new GroupBarPlot(array($plot2013,$plot2014, $plot2015));

I think that in the second one arrays are passed as arguments where as in first one just array name is passed as string. How can I pass the array created inside the for loop to the jpgraph function?


Solution

  • Not tested, but try the code below.

    $j=0;
    for($year=$start;$year<=$end;$year++, $j++){
      ${"plot".$year}=new BarPlot(${"y".$year});
    
      ${"plot".$year}->SetFillColor($color_array[$j]);
    
    
      $plots[] = ${"plot".$year};
    }
    $gbplot = new GroupBarPlot($plots);