Menu

can't get data from another php file

muppet77
2010-12-09
2012-09-07
  • muppet77

    muppet77 - 2010-12-09

    i have 12 different rainfall totals stored as variables in 12 different files
    - 1 for each month.

    the numbers display on the graph, but the wrong ones.

    the top part of my code is

    <?php

    include 'jan.php';

    include 'feb.php';

    include 'mar.php';

    include 'apr.php';

    include 'may.php';

    include 'jun.php';

    include 'jul.php';

    include 'aug.php';

    include 'sep.php';

    include 'oct.php';

    include 'nov.php';

    include 'dec.php';

    PHPlot Example: Two plots on one image

    require ($_SERVER . '/phplot.php');

    $data2 = array( # Data array for rain plot:

    array('JAN', $out_tot_rainjan), array('FEB', $out_tot_rainfeb), array('MAR',
    $out_tot_rainmar),

    array('APR', $out_tot_rainapr), array('MAY', $out_tot_rainmay), array('JUN',
    $out_tot_rainjun),

    array('JUL', $out_tot_rainjul), array('AUG', $out_tot_rainaug), array('SEP',
    $out_tot_rainsep),

    array('OCT', $out_tot_rainoct), array('NOV', $out_tot_rainnov), array('DEC',
    $out_tot_raindec),

    );

    the files can be found at www.maidenerleghweather.com/jan.php etc

    the graph is /graph2.php

    any idea what's going on?

    PS: I ONLY EXPECT JAN, FEB, MAR AND AUG TO SHOW.

    JAN is accurate, the rest are out.

    eg AUG should be 113 ?????

     
  • muppet77

    muppet77 - 2010-12-09

    ... and when i add

    print $out_tot_rainaug;

    to the august file it prints 113.

    so that part is correct???

     
  • lbayuk

    lbayuk - 2010-12-09

    Hi, we are going to need to see some more information, such as what is in
    those data files. (Your link doesn't work, as "jan.php" is probably running on
    your server with PHP so we can't see what is in it.) Also what is your data
    type (SetDataType), plot type, etc? If you could include the whole script (if
    short enough) that would be even better. Or, if you are debugging with print,
    try checking the data array you are giving SetDataValues, for example
    print_r($data2) instead of producing the graph.

     
  • muppet77

    muppet77 - 2010-12-10

    aug.php cuts values from a file called all.txt

    (in fact all the month files do - jan, feb etc)

    when i add print $out_tot_rainaug;

    to the august file it prints 113.

    the aug file is

    <?php
    
    putenv("TZ=Europe/London");
    $today = mktime(0, 0, 0, date("m"), date("d"), date("y"));
    $mo = 8; 
    $daysinmonth = 31;
    
    
    $filename = "all.txt";
    $fh = fopen($filename, 'r') or die("Couldn't open $filename");
    
    $out_max_temp = '-100';
    $out_min_temp = '100';
    $out_tot_bar = '0';
    
    
    while (!feof($fh))
     {
      $wldata= fgets($fh);
      if (strpos($wldata,"Rain")!== FALSE)  //SKIP headers - "Rain" is found in both lines
         {} 
       else
        {
         if (strpos($wldata,"0") !== FALSE) // ALWAYS a zero somewhere in the data
          {
           $wlarray = array() ;
           $wlarray = explode("\t",$wldata);
    
    if ($wlarray[3] > $out_max_temp && substr($wlarray[0],3,2) == $mo)
             $out_max_temp = $wlarray[3];
    
    if ($wlarray[4] < $out_min_temp && $wlarray[4] != "---" && substr($wlarray[0],3,2) == $mo)
              $out_min_temp = $wlarray[4];
    
    if ($wlarray[2] != "---" && substr($wlarray[0],3,2) == $mo)
          {    $out_tot_temp = $out_tot_temp + $wlarray[2];  
               $counta = $counta + 1;}
    
    if ($wlarray[16] != "---" && substr($wlarray[0],3,2) == $mo)
              $out_tot_rain = $out_tot_rain + $wlarray[16];
    
    if ($wlarray[15] != "---" && substr($wlarray[0],3,2) == $mo)
          {    $out_tot_bar = $out_tot_bar + $wlarray[15];  
               $countb = $countb + 1;}
    
    if ($wlarray[7] != "---" && substr($wlarray[0],3,2) == $mo)
          {    $out_tot_wind = $out_tot_wind + $wlarray[7];  
               $countc = $countc + 1;}
    if ($wlarray[5] != "---" && substr($wlarray[0],3,2) == $mo)
          {    $out_tot_hum = $out_tot_hum + $wlarray[5];  
               $countd = $countd + 1;}
    if ($wlarray[20] != "---" && substr($wlarray[0],3,2) == $mo)
          {    $in_temp = $in_temp + $wlarray[20];  
               $counte = $counte + 1;}
    if (substr($wlarray[0],3,2) == $mo)
          {    $click = $click + 1;}
          }
        }
     }
    fclose($fh);
    
    $out_tot_temp = round(($out_tot_temp / $counta) ,1);
    $out_tot_bar = round(($out_tot_bar / $countb),1 );
    $out_tot_wind = round(($out_tot_wind / $countc),1 );
    $out_tot_hum = round(($out_tot_hum / $countd),1 );
    $in_temp = round(($in_temp / $counte),1 );
    $days = $click / 4 / 24 / $daysinmonth;
    $out_tot_rain = $out_tot_rain / $days;
    $out_tot_rain = round($out_tot_rain,1);
    
    $out_tot_tempaug = $out_tot_temp; 
    $out_tot_baraug = $out_tot_bar; 
    $out_tot_windaug = $out_tot_wind; 
    $out_tot_humaug = $out_tot_hum; 
    $in_tempaug = $in_temp; 
    $out_tot_rainaug = $out_tot_rain; 
    $out_max_tempaug = $out_max_temp;
    $out_min_tempaug = $out_min_temp;
    
    
    
    
    ?>
    

    my phplot code called graph2 is

    <?php
    include 'jan.php';
    include 'feb.php';
    include 'mar.php';
    include 'apr.php';
    include 'may.php';
    include 'jun.php';
    include 'jul.php';
    include 'aug.php';
    include 'sep.php';
    include 'oct.php';
    include 'nov.php';
    include 'dec.php';
    
    
    # PHPlot Example: Two plots on one image
    require ($_SERVER['DOCUMENT_ROOT'] . '/phplot.php');
    
    
    $data2 = array(        # Data array for rain plot: 
      array('JAN', $out_tot_rainjan),  array('FEB', $out_tot_rainfeb),  array('MAR', $out_tot_rainmar),
      array('APR', $out_tot_rainapr),  array('MAY', $out_tot_rainmay),  array('JUN', $out_tot_rainjun),
      array('JUL', $out_tot_rainjul),  array('AUG', $out_tot_rainaug),  array('SEP', $out_tot_rainsep),
      array('OCT', $out_tot_rainoct),  array('NOV', $out_tot_rainnov),  array('DEC', $out_tot_raindec),
      );
    
    
    
    
    $plot = new PHPlot(800,600);
    //$plot->SetImageBorderType('plain');
    
    # Disable auto-output:
    $plot->SetPrintImage(0);
    
    # There is only one title: it is outside both plot areas.
    $plot->SetTitle('Climate graph for Maiden Erlegh School since 25/11/09');
    
    
    # Set up area for rain plot:
    $plot->SetPlotAreaPixels(80, 40, 740, 350);
    
    # Do the rain plot:
    $plot->SetDataType('text-data');
    $plot->SetDataValues($data2);
    $plot->SetPlotAreaWorld(NULL, 0, NULL, 200);
    $plot->SetDataColors(array('blue'));
    $plot->SetXTickLabelPos('none');
    $plot->SetXTickPos('none');
    $plot->SetYTickIncrement(20);
    $plot->SetYTitle("Rainfall in mm", plotright);
    $plot->SetXTitle("Month");
    $plot->SetDrawYGrid(false);
    $plot->SetXAxisPosition(0);
    $plot->SetYTickPos(plotright);
    $plot->SetYAxisPosition(12);
    $plot->SetYTickLabelPos(plotright);
    $plot->SetPlotType('bars');
    $plot->SetYTitleColor(blue);
    $plot->SetTickColor(blue);
    $plot->SetTextColor(blue);
    $plot->SetYDataLabelPos(plotin);
    
    $plot->DrawGraph();
    
    
    # Output the image now:
    $plot->PrintImage();
    
     
  • lbayuk

    lbayuk - 2010-12-10

    You are missing a number of quote marks around constant strings in graph2.php,
    like 'plotin', 'blue', 'plotright'. Can I assume those happened when you
    posted this, and the actual script is correct? (That is, you are have error
    checking on and have made sure you aren't getting PHP syntax errors or
    undefined variable/constants warnings.)

    Also, can you tell me what PHPlot version you are using?

    I took your code, fixed the quotes, and substituted constant values for the 12
    out_tot_rain* (first 3 values, aug value, and the rest are empty strings), and
    I get the same picture as you, except the aug number is what I put in. So that
    points again to your data values. What I would try at this point is insert
    these 3 lines just before SetDataValues($data2):

    header("Content-type: text/plain");
    print_r($data2);
    exit();
    

    Now go to the script's URL and you should see a listing of your data array
    exactly as PHPlot will see it. Do the values and format look correct?

     
  • muppet77

    muppet77 - 2010-12-11

    no - it shows Aug to be 15.4 not 113?

     
  • muppet77

    muppet77 - 2010-12-11

    / $Id: phplot.php,v 1.201 2010/10/03 21:57:09 lbayuk Exp $ /

    /*

    • PHPLOT Version 5.2.0
     
  • muppet77

    muppet77 - 2010-12-12

    bit of a breakthrough.

    i think that the variables from each month file are confusing each other as
    they have identical names.

    //include 'jan.php';

    //include 'feb.php';

    //include 'mar.php';

    //include 'apr.php';

    //include 'may.php';

    //include 'jun.php';

    //include 'jul.php';

    include 'aug.php';

    //include 'sep.php';

    //include 'oct.php';

    //include 'nov.php';

    //include 'dec.php';

    when i just select the aug file it returns a bar in the graph as 113 - which
    is correct.

    i will adjust variable names.

    check it out at

    http://www.maidenerleghweather.com/graph2.php

     
  • muppet77

    muppet77 - 2010-12-12

    ok - no idea what's going on now.

    all variables have different names.

    the latest effort is at http://www.maidenerleghweather.com/graph.php

    the max and min seem to be ok. the averages are wrong as are the rainfall.
    remmeber as a cgeck aug should be 113 rain.

    i have the graph.php code as

    <?php
    include 'jan.php';
    include 'feb.php';
    include 'mar.php';
    include 'apr.php';
    include 'may.php';
    include 'jun.php';
    include 'jul.php';
    include 'aug.php';
    include 'sep.php';
    include 'oct.php';
    include 'nov.php';
    include 'dec.php';
    
    
    # PHPlot Example: Two plots on one image
    require ($_SERVER['DOCUMENT_ROOT'] . '/phplot.php');
    
    
    $data1 = array(        # Data array for average plot
      array('JAN', $out_tot_tempjan),  array('FEB', $out_tot_tempfeb),  array('MAR', $out_tot_tempmar),
      array('APR', $out_tot_tempapr),  array('MAY', $out_tot_tempmay),  array('JUN', $out_tot_tempjun),
      array('JUL', $out_tot_tempjul),  array('AUG', $out_tot_tempaug),  array('SEP', $out_tot_tempsep),
      array('OCT', $out_tot_tempoct),  array('NOV', $out_tot_tempnov),  array('DEC', $out_tot_tempdec),
      );
    $data2 = array(        # Data array for rain plot: 
      array('JAN', $out_tot_rainjan),  array('FEB', $out_tot_rainfeb),  array('MAR', $out_tot_rainmar),
      array('APR', $out_tot_rainapr),  array('MAY', $out_tot_rainmay),  array('JUN', $out_tot_rainjun),
      array('JUL', $out_tot_rainjul),  array('AUG', $out_tot_rainaug),  array('SEP', $out_tot_rainsep),
      array('OCT', $out_tot_rainoct),  array('NOV', $out_tot_rainnov),  array('DEC', $out_tot_raindec),
      );
    
    $data3 = array(        # Data array for max plot: 
      array('JAN', $out_max_tempjan),  array('FEB', $out_max_tempfeb),  array('MAR', $out_max_tempmar),
      array('APR', $out_max_tempapr),  array('MAY', $out_max_tempmay),  array('JUN', $out_max_tempjun),
      array('JUL', $out_max_tempjul),  array('AUG', $out_max_tempaug),  array('SEP', $out_max_tempsep),
      array('OCT', $out_max_tempoct),  array('NOV', $out_max_tempnov),  array('DEC', $out_max_tempdec),
      );
    
    $data4 = array(        # Data array for min plot: 
      array('JAN', $out_min_tempjan),  array('FEB', $out_min_tempfeb),  array('MAR', $out_min_tempmar),
      array('APR', $out_min_tempapr),  array('MAY', $out_min_tempmay),  array('JUN', $out_min_tempjun),
      array('JUL', $out_min_tempjul),  array('AUG', $out_min_tempaug),  array('SEP', $out_min_tempsep),
      array('OCT', $out_min_tempoct),  array('NOV', $out_min_tempnov),  array('DEC', $out_min_tempdec),
      );
    
    
    
    $plot = new PHPlot(800,600);
    //$plot->SetImageBorderType('plain');
    
    # Disable auto-output:
    $plot->SetPrintImage(0);
    
    # There is only one title: it is outside both plot areas.
    $plot->SetTitle('Climate graph for Maiden Erlegh School since 25/11/09');
    
    
    # Set up area for average plot:
    $plot->SetPlotAreaPixels(80, 40, 740, 350);
    
    # Do the average plot:
    $plot->SetDataType('text-data');
    $plot->SetDataValues($data1);
    $plot->SetPlotAreaWorld(NULL, -50, NULL, 30);
    $plot->SetDataColors(array('orange'));
    $plot->SetXTickLabelPos('none');
    $plot->SetXTickPos('none');
    $plot->SetYTickIncrement(5);
    $plot->SetYTitle("Max, Min and Average Temperature in C");
    $plot->SetDrawYGrid(false);
    $plot->SetLineWidths(2);
    $plot->SetXAxisPosition(0);
    $plot->SetYTickLabelPos(plotleft);
    $plot->SetYTickPos(plotleft);
    $plot->SetYTickLength(5);
    //$plot->SetPlotType('lines');
    $plot->SetDrawYGrid(true);
    $plot->SetLightGridColor(red);
    $plot->SetYTitleColor(red);
    $plot->SetTickColor(red);
    $plot->SetTextColor(red);
    $plot->SetGridColor(red);
    $headers = array('max', 'min', 'average', 'rainfall');
    
    $plot->DrawGraph();
    
    # Set up area for max plot:
    $plot->SetPlotAreaPixels(80, 40, 740, 350);
    
    # Do the max plot:
    $plot->SetDataType('text-data');
    $plot->SetDataValues($data3);
    $plot->SetPlotAreaWorld(NULL, -50, NULL, 30);
    $plot->SetDataColors(array('red'));
    $plot->SetXTickLabelPos('none');
    $plot->SetXTickPos('none');
    $plot->SetYTickIncrement(5);
    $plot->SetYTitle("");
    $plot->SetDrawYGrid(false);
    $plot->SetLineWidths(2);
    $plot->SetXAxisPosition(0);
    $plot->SetYTickLabelPos(plotleft);
    $plot->SetYTickPos(plotleft);
    $plot->SetYTickLength(5);
    //$plot->SetPlotType('lines');
    $plot->SetDrawYGrid(true);
    $plot->SetLightGridColor(red);
    $plot->SetYTitleColor(red);
    $plot->SetTickColor(red);
    $plot->SetTextColor(red);
    $plot->SetGridColor(red);
    
    $plot->DrawGraph();
    
    # Set up area for min plot:
    $plot->SetPlotAreaPixels(80, 40, 740, 350);
    
    # Do the min plot:
    $plot->SetDataType('text-data');
    $plot->SetDataValues($data4);
    $plot->SetPlotAreaWorld(NULL, -50, NULL, 30);
    $plot->SetDataColors(array('SkyBlue'));
    $plot->SetXTickLabelPos('none');
    $plot->SetXTickPos('none');
    $plot->SetYTickIncrement(5);
    $plot->SetYTitle("");
    $plot->SetDrawYGrid(false);
    $plot->SetLineWidths(2);
    $plot->SetXAxisPosition(0);
    $plot->SetYTickLabelPos(plotleft);
    $plot->SetYTickPos(plotleft);
    $plot->SetYTickLength(5);
    //$plot->SetPlotType('lines');
    $plot->SetDrawYGrid(true);
    $plot->SetLightGridColor(red);
    $plot->SetYTitleColor(red);
    $plot->SetTickColor(red);
    $plot->SetTextColor(red);
    $plot->SetGridColor(red);
    
    $plot->DrawGraph();
    
    
    
    # Set up area for rain plot:
    $plot->SetPlotAreaPixels(80, 40, 740, 350);
    
    # Do the rain plot:
    $plot->SetDataType('text-data');
    $plot->SetDataValues($data2);
    $plot->SetPlotAreaWorld(NULL, 0, NULL, 200);
    $plot->SetDataColors(array('blue'));
    $plot->SetXTickLabelPos('none');
    $plot->SetXTickPos('none');
    $plot->SetYTickIncrement(20);
    $plot->SetYTitle("Rainfall in mm", plotright);
    $plot->SetXTitle("Month");
    $plot->SetDrawYGrid(false);
    $plot->SetXAxisPosition(0);
    $plot->SetYTickPos(plotright);
    $plot->SetYAxisPosition(12);
    $plot->SetYTickLabelPos(plotright);
    $plot->SetPlotType('bars');
    $plot->SetYTitleColor(blue);
    $plot->SetTickColor(blue);
    $plot->SetTextColor(blue);
    $plot->SetYDataLabelPos(plotin);
    
    $plot->DrawGraph();
    
    
    # Output the image now:
    $plot->PrintImage();
    
     
  • muppet77

    muppet77 - 2010-12-12

    worked it out - still duplicate variable names in different files causing
    error.

    will sort it tonight.

    thanks for looking and helping - hope this helps someone else.

    be thorough!!!!

     
  • lbayuk

    lbayuk - 2010-12-13

    Sorry for not replying before now - my ISP seems to have taken the weekend off
    (unable to reach sourceforge.net directly, but the forum emails were getting
    through because they don't go direct to me from sourceforge.net... So I could
    see what you were posting but could not reply). I'm glad you found the
    problem. I was going to suggest looking more carefully at the code, or even
    trying to find a better way, as having 12 almost-the-same copies of scripts
    and variable names seems to be asking for trouble.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.