Menu

Help with external data file

gt651
2011-04-09
2012-09-07
  • gt651

    gt651 - 2011-04-09

    I've been trying to read the data in this code from an external .csv file but
    I need help with what the external data file should look like plus how do I
    read it in so it works with this code.

    Thanks Gary

    SetPlotAreaWorld(null, 29.00, null, 30.00); $plot->SetDataValues($example_data); //Turn off X axis ticks and labels because they get in the way: $plot->SetXTickLabelPos('none'); $plot->SetXTickPos('none'); //Draw it $plot->DrawGraph(); ?>
     
  • lbayuk

    lbayuk - 2011-04-09

    You can put your data into a file that looks like this. I called it
    "data.csv":

    12am,29.55
    1am,29.58
    2am,29.66
    ...
    

    Then you read it from your script, by replacing the assignment to
    $example_data with something like this:

    //Read data from file:
    $f = fopen('data.csv', 'r');
    if (!$f) exit(1); // Need error handling here
    $example_data = array();
    while (($row = fgetcsv($f)) !== FALSE) $example_data[] = $row;
    fclose($f);
    
     
  • gt651

    gt651 - 2011-04-09

    Wow thanks so much - it works perfect.

    Now on to updating the data file once an hour.

     
  • lbayuk

    lbayuk - 2011-04-09

    Now on to updating the data file once an hour.

    Be careful of race conditions. If you are updating the file at the exact time
    the script is also reading it, you might get an error opening the file, or end
    up reading corrupt data.

     
  • gt651

    gt651 - 2011-04-10

    I haven't got to the race conditions yet but I will when I get everything else
    working.

    I'm now stuck trying to add a newline when I append hourly data to data.csv.

    When I add data to data.csv it adds it like so to the bottom of the file
    without a newline.

    4pm,29.66

    5pm,29.78

    6pm,29.85

    7pm,29.60

    8pm,29.45

    9pm,29.55

    10pm,29.58

    11pm,29.663pm,29.21\n5pm,29.215pm,29.215pm,29.215pm,29.215pm,29.21

    Any help here would be appreciated - Thanks Gary

    pressure_in; // combine current hour and pressure $new_time_pressure = $time.$pressure; // append a newline $new_time_pressure .= "\n"; // append to data file $f = fopen($file,'a'); fwrite($f,$new_time_pressure); fclose($f); ?>
     
  • lbayuk

    lbayuk - 2011-04-11

    file_put_contents($file, implode("\r\n", $contents));

    This is putting a newline between each record, but not after the last record.
    You need another newline at the end of the file (before you append your data).

     
  • gt651

    gt651 - 2011-04-11

    Would this do it?

    $new_time_pressure = "\n" . $new_time_pressure;

     
  • lbayuk

    lbayuk - 2011-04-11

    Yes, assuming you left the other line in that appends a newline also. But you
    should be able to do a lot better. Why not append your new data to the
    $contents array at the top of your code, then skip the whole re-open in append
    mode and write?

    You would be writing it out with the new data at the top, like this:

    file_put_contents($file, implode("\r\n", $contents) . "\r\n");
    
     
  • gt651

    gt651 - 2011-04-11

    OK I'll try that. I just tried this (like you said) and it is looking better
    now.

    // append a newline

    $new_time_pressure = "\n" . $new_time_pressure ."\n";

     
  • gt651

    gt651 - 2011-04-11

    OK I used your suggestion and it looks good.

    This; file_put_contents($file, implode("\r\n", $contents) . "\r\n");

    I made it a cron job set to run every 12 mins after the hour so we'll see.

    I want to work on the graph but here is where I'm headed:

    http://www.mnlakecams.com/simpleplot.php

    Thanks much Gary

     

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.