Welcome to the Cumulus Support forum.

Latest Cumulus MX V3 release 3.28.6 (build 3283) - 21 March 2024

Cumulus MX V4 beta test release 4.0.0 (build 4018) - 28 March 2024

Legacy Cumulus 1 release v1.9.4 (build 1099) - 28 November 2014 (a patch is available for 1.9.4 build 1099 that extends the date range of drop-down menus to 2030)

Download the Software (Cumulus MX / Cumulus 1 and other related items) from the Wiki

Need help with a php script

Other discussion about creating web sites for Cumulus that doesn't have a specific subforum

Moderator: daj

Post Reply
W2SWR
Posts: 18
Joined: Thu 08 Mar 2012 2:31 am
Weather Station: WH-1090
Operating System: vista
Location: Little River, SC.

Need help with a php script

Post by W2SWR »

I am trying to delete the first variable value using a php script. I thought using an unset would do the trick but I cant make it work. The file is called rain.log . Each day the daily rain is being pulled from realtime.txt at 11:59pm with a cron job and added to the end of my rain.log so I can get a 6 day total. That part I have working but I need to delete the first value (day6) in rain.log as I add a new day at the end.

Example:

Code: Select all

1.00 2.00 3.00 4.00 5.00 6.00
Once the php script is finished, this should be the result;

Code: Select all

2.00 3.00 4.00 5.00 6.00 7.00
1.00 is dropped and 7.00 is added

Thanks all!

Mike
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need help with a php script

Post by beteljuice »

How are you adding the new (end) value ?
Image
......................Imagine, what you will KNOW tomorrow !
W2SWR
Posts: 18
Joined: Thu 08 Mar 2012 2:31 am
Weather Station: WH-1090
Operating System: vista
Location: Little River, SC.

Re: Need help with a php script

Post by W2SWR »

Its long but heres part of it. It is based on TNET's realtimelog.php

Code: Select all

$DATA = preg_split('/ +/', $rawdata[0]);
$fp = fopen( $SITE['datafile'] , 'a' );
if ($fp) {
  $info = "";     
    $first = 1;     
    foreach($DATA as $key) {
        $info .= " ";
$info .= $key; }
 fwrite($fp, $info . "\n" ) ;
    fclose($fp);
}
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need help with a php script

Post by beteljuice »

Something along the lines of:

Code: Select all

<?php

$raw = file('realtime.txt');
$rawdata = array();
$rawdata = explode ( ' ', $raw[0] );

$latest_rain = $rawdata[9];

$rainraw = file('rain.log');
$rainraw[0] = rtrim($rainraw[0]);
$raindata = array();
$raindata = explode(' ', $rainraw[0]);
$x = count($raindata);
$output = "";

if($x > 5){
   $xFrom = 1;
   $xTo = 6;
} else {
   $xFrom = 0;
   $xTo = $x;
}

for( $rebuild = $xFrom; $rebuild < $xTo ; $rebuild++) {
   $output .= $raindata[$rebuild]. ' ';
}
$output .= $latest_rain. "\n";

$fh = fopen("rain.log", "w");
fwrite($fh, $output);
fclose($fh);

?>
Edit #3 - rewritten with a clearer head on !!!

Rays (gemini) code has more belts 'n' braces and is a better 'model'.
Last edited by beteljuice on Tue 10 Apr 2012 11:59 pm, edited 3 times in total.
Image
......................Imagine, what you will KNOW tomorrow !
W2SWR
Posts: 18
Joined: Thu 08 Mar 2012 2:31 am
Weather Station: WH-1090
Operating System: vista
Location: Little River, SC.

Re: Need help with a php script

Post by W2SWR »

Code: Select all

$x2 = ($x <6 ? $x : 6);
Gave me a
Parse error: syntax error, unexpected T_VARIABLE
Not sure what you are doing here. I am not too good with php yet.
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need help with a php script

Post by beteljuice »

Forgot the ; at the end of

$output = "";

Pick up latest rainfall from realtime.txt

read rainfile.log / get its values

build a an array for 'old' rainlog values.
Write a new 'line' by reading 'old' values [1] upto [5] (it may not have been fully populated)
then adding latest to end of new 'line'.

Write new line as new rainfile.log

Try it now ...
Image
......................Imagine, what you will KNOW tomorrow !
W2SWR
Posts: 18
Joined: Thu 08 Mar 2012 2:31 am
Weather Station: WH-1090
Operating System: vista
Location: Little River, SC.

Re: Need help with a php script

Post by W2SWR »

Wow, I missed the ; too. Now it gets as far as dumping the log then it is looking for a parameter.
Warning: explode() expects parameter 2 to be string, array given in /home/bla/public_html/bla/test.php on line 5

Warning: explode() expects parameter 2 to be string, array given in /home/bla/public_html/bla/test.php on line 10
Image
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need help with a php script

Post by beteljuice »

Next error .........

$raw = file('realtime.txt);

missed end '

$raw = file('realtime.txt');

The errors suggest that the 'files' have not been read correctly, but it's saying that an array (not a string) has been passed .... you have put the correct paths for YOUR files ?

When I've got my thinking head back on I'll creare some artifical data and see what I've said wrong.

PS. Got a link to the test file and your data files ?

PPS: Try:

$rawdata = explode ( ' ', $raw[0] );

and

$raindata = explode(' ', $rainraw[0]);

04:41 local .... going byebye
Image
......................Imagine, what you will KNOW tomorrow !
gemini06720
Posts: 1700
Joined: Mon 10 Aug 2009 10:16 pm
Weather Station: No weather station
Operating System: No operating system
Location: World...

Re: Need help with a php script

Post by gemini06720 »

Mike, here is an excerpt of a script I have been using to produce similar results:

Code: Select all

<?php
##############################################################################################
#
//error_reporting(E_NOTICE);
//error_reporting(E_ERROR);
error_reporting(E_ALL);
//error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
//error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
//ini_set('log_errors', 1);
//ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
#
##############################################################################################

$DoDebug = true;

$realtime = trim('realtime.txt');
$rainfile = trim('rain.log');
if (!file_exists($realtime)) { exit("ERROR: File '$realtime' was not found! Exiting!");}
if (!file_exists($rainfile)) { exit("ERROR: File '$rainfile' was not found! Exiting!");}

// getting data from 'realtime.txt' file
$realtimedata = array();
$raw = trim(file_get_contents($realtime));
// splitting string into an array
$realtimedata = explode(' ', $raw);
$rain = $realtimedata[9]; // rain today
$uomRain = $realtimedata[16]; // rain unit - not needed
if ($DoDebug) { print "<br /><br />realtime file:"; var_dump ($realtimedata); }
if ($DoDebug) { print "<br />Rain value from realtime file: $rain $uomRain<br />"; }

// getting rain data from 'rain.log' file
$raindata = array();
$raw = trim(file_get_contents($rainfile));
// splitting string into an array
$raindata = explode (' ', $raw);
if ($DoDebug) { print "<br />Old rain-log file:"; var_dump ($raindata); }

// shifting first value off - shortening array by one element
$temp = array_shift($raindata);
if ($DoDebug) { print "<br />Shortened rain-log file:"; var_dump($raindata); }

// pushing new variable onto the end of array - increasing length of array by one element
$temp = array_push($raindata, $rain);
if ($DoDebug) { print "<br />New rain-log file:"; var_dump($raindata); }

// join array elements into a string
$output = implode(' ', $raindata);
if ($DoDebug) { print "<br />New data to be written to rain-log file: $output<br />"; }

// writing string to file - simple option
if (file_put_contents($rainfile, $output) === FALSE) { exit("ERROR: Cannot write to file '$rainfile'! Exiting!"); }
if ($DoDebug) { print "<br />Success: '$output' was written to file '$rainfile'"; }

// writing string to file - alternate option
//if (is_writable($rainfile)) {
//  if (!$handle = fopen($rainfile, 'w')) { exit("ERROR: Cannot open file '$rainfile'! Exiting!"); }
//  if (fwrite($handle, $output) === FALSE) { exit("ERROR: Cannot write to file '$rainfile'! Exiting!"); }
//  if ($DoDebug) { print "<br />Success: '$output' was written to file '$rainfile'"; }
//  fclose($handle);
//}
//else { print "WARNING: The file '$rainfile' is not writable!"; }

exit;

##############################################################################################
?>
The script will need to be edited and adapted for your purpose...

I usually design (or modify) my scripts to contain tests for errors and to ensure the required files are there...
W2SWR
Posts: 18
Joined: Thu 08 Mar 2012 2:31 am
Weather Station: WH-1090
Operating System: vista
Location: Little River, SC.

Re: Need help with a php script

Post by W2SWR »

Thanks Ray,


Actually in work right out of the box! I will go threw it to see what else the file is trying to do unneeded. I had to add

Code: Select all

$HOME = "/home/path/to/log/and/txt/";
chdir($HOME);
so I could move the php above public_html. I want to trigger it once a day by a cron job, not by every Tom, Dick and Google ;-)

Thank you very much for the help
Image
W2SWR
Posts: 18
Joined: Thu 08 Mar 2012 2:31 am
Weather Station: WH-1090
Operating System: vista
Location: Little River, SC.

Re: Need help with a php script

Post by W2SWR »

And thank you too beteljuice,

I am still going to work on your script. It helps me learn from the experience. Right now it is trying to explode the literal term "rain.log" and "realtime.txt" and injecting the literal word "Array" into rain.log. I tried
$rawdata = explode ( ' ', $raw[0] );

and

$raindata = explode(' ', $rainraw[0]);
which didn't resolve it. but I will keep on it.
Image
gemini06720
Posts: 1700
Joined: Mon 10 Aug 2009 10:16 pm
Weather Station: No weather station
Operating System: No operating system
Location: World...

Re: Need help with a php script

Post by gemini06720 »

Mike, as I wrote, the code I posted is just part (a small section) of an older script. Normally, the following bit of code should/would also have been included:

Code: Select all

$DoDebug = true;

$realtime = 'realtime.txt';
$rainfile = 'rain.log';
$webDir   = 'cumulus';

// Obtain basic environment
if (!isset($_SERVER['DOCUMENT_ROOT'])) {
  $path_trans = str_replace('\\\\', '/', $_SERVER['PATH_TRANSLATED']);
  $ROOTDIR = substr($path_trans, 0, 0 - strlen($_SERVER['PHP_SELF']));
} 
else { $ROOTDIR = $_SERVER['DOCUMENT_ROOT']; }
$fileDir = realpath($ROOTDIR.'\\'.trim($webDir)).'\\';

$realtime = $fileDir.trim($realtime);
$rainfile = $fileDir.trim($rainfile);
if (!file_exists($realtime)) { exit("ERROR: File '$realtime' was not found! Exiting!");}
if (!file_exists($rainfile)) { exit("ERROR: File '$rainfile' was not found! Exiting!");}
...
With these extra lines, the script identifies the location of the root directory and adjust accordingly - I do not think (although I am not sure) if a 'chdir();' would still be needed.

But then, that portion of the code would not work for you as you appear to be placing your script above the root directory...

It is odd to read about putting some code/script above the highest directory (in your case the 'public_html' directory - on my Apache servers, the highest directory is identified as 'htdocs'). Although I can access/see directories above the 'root' directory, I was taught by an excellent instructor many years ago to never go above the 'root' directory - thus my 'habit' of leaving the 'htdocs' root directory empty except for an '.htaccess' control file and a few 'index.xxx' redirection files, all the scripts/templates are located into sub-directories.
User avatar
beteljuice
Posts: 3292
Joined: Tue 09 Dec 2008 1:37 pm
Weather Station: None !
Operating System: W10 - Threadripper 16core, etc
Location: Dudley, West Midlands, UK

Re: Need help with a php script

Post by beteljuice »

The beteljuice has just about recovered some brain cells. Original post rewritten (and tested !)
Image
......................Imagine, what you will KNOW tomorrow !
W2SWR
Posts: 18
Joined: Thu 08 Mar 2012 2:31 am
Weather Station: WH-1090
Operating System: vista
Location: Little River, SC.

Re: Need help with a php script

Post by W2SWR »

Hi Ray,
It is odd to read about putting some code/script above the highest directory
Its working fine above the root directory. I have it up there because I need to avoid bots hitting the file (robots.txt is only obeyed by legit bots!) and triggering it thus messing up the days. I have a cron job triggering it once a day. If it was to trigger twice, then the log is off a day or worse if it rains lets say .25 inches that day and it logs that .25 twice.

Mike
Image
Post Reply