adding a date column

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
trauch
Forum Newbie
Posts: 14
Joined: Thu Dec 11, 2008 4:50 pm

adding a date column

Post 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.
trauch
Forum Newbie
Posts: 14
Joined: Thu Dec 11, 2008 4:50 pm

Re: adding a date column

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: adding a date column

Post by pickle »

What have you tried? Have you looked at fopen()?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply