Page 1 of 1

adding a date column

Posted: Mon Dec 22, 2008 10:50 am
by trauch
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


I currently have a script that adds numbers from multiple files and then outputs the sum (a single number like 28449358) to a new text file named as the current date (ie 2008-12-22). Although this is working I'd like to change the code so that the sum is appended to a file called total.txt. However, in addition to the number there needs to be a date stamp separated by a ";" (ie 2008-12-22;28449358). This would be appended to each day. Btw, Im using php4.

Here's my existing cod

Code: Select all

<?php
 
 
// Make an array of the filenames to sum
$fileNames = array('textfile.txt', 'textfile2.txt', 'textfile3.txt');
 
// Open the file and zero out the net sum
$fp = fopen('textmaster.txt', 'a');
$netSum = 0;
 
// Add the numbers and append to the file
foreach($fileNames as $fileName) {
   $content = str_replace(' ', '', file($fileName)); 
   $sum = array_sum($content);
   echo 'Sum of "' . $fileName . ': ' . $sum;
   $netSum += $sum;
   fwrite($fp, $sum);
}
 
// Clean up
fclose($fp);
 
// Create the day file
$fw = fopen(date('Y-n-j') ';');
fwrite($fw, $netSum);
fclose($fw)
 
 
?>
e


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: adding a date column

Posted: Mon Dec 22, 2008 12:55 pm
by trauch
To elaborate, my problem is that currently my output is a text file whose filename will be the current date and whose contents will be a single number (sum from script).

What I want is the output to be saved (appended) to an existing file (total.txt) and instead of simply providing the number, to also include a date stamp. So each day the output would be (2008-12-22;238794)
Over a series of days, the resulting total.txt would look like:

2008-12-22;238794
2008-12-23;525355
2008-12-24;636633
2008-12-25;455747

Re: adding a date column

Posted: Mon Dec 22, 2008 2:29 pm
by pickle
What have you tried? Have you looked at fopen()?