
trauch
Members-
Posts
19 -
Joined
-
Last visited
Never
Profile Information
-
Gender
Not Telling
trauch's Achievements

Newbie (1/5)
0
Reputation
-
Can anyone help me with a script to write the date. Im using php4 and am stuck. I have a script that adds a series of numbers and outputs it to a new file. 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 something like - 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
-
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
-
I currently have a script that adds numbers from multiple files and then outputsthe 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 code <?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) ?>
-
all I can say is WOW! Thanks
-
I wish I could. Im on a shared server and my host has control over upgrading so...
-
With the help of genericnumber1 I'm almost there but I would appreciate any input. Since Im using php4 I cant use file_put_contents and am still trying to figure out how to save the file as the current date (2008-12-22.txt). I know I have to use fwrite but am not sure how to do this. Here's what I have thus far. <?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 fwrite(date('Y-n-j') . '.txt', $netSum); ?>
-
I removed the & and it seems to be adding the individual text files. Unfortunately php4 also does not recognize file_put_contents. Do you know how to do this with fwrite or some other way with php4?
-
this looks really promising but I got an error on line 10 Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' Line 10 says: foreach($fileNames as &$fileName) { Any ideas? Btw, Im using php4.
-
So as to not confuse anyone regarding my progress I have started a new thread. I'm working on a script that will perform a few tasks 1) Load multiple text files each of which contains a line of numbers 2) In each text file remove white space and then add all lines 3) Append the contents of all of these text files to another text file 4) Add all numbers in the resulting text file 5) Save that resulting number as a new text file whose name is generated as the current date. (ie 2008-12-22.txt) I've got some messy code below but would appreciate any assistance. <?php $data1 = file('textfile1.txt'); $data1 = str_replace(' ','',$data1); echo 'sum($data1) = ' . array_sum($data1) . "\n"; $sum = array_sum($data1) . "\n"; $data2 = file('textfile2.txt'); $data2 = str_replace(' ','',$data2); echo 'sum($data2) = ' . array_sum($data2) . "\n"; $sum2 = array_sum($data2) . "\n"; $data3 = file('textfile3.txt'); $data3 = str_replace(' ','',$data3); echo 'sum($data3) = ' . array_sum($data3) . "\n"; $sum3 = array_sum($data3) . "\n"; $fp = fopen('textmaster.txt', 'a'); fwrite($fp, $sum); fwrite($fp, $sum2); fwrite($fp, $sum3); fclose($fp); ?>
-
I am trying to open, read, copy and then append the contents of multiple text or csv files to the bottom of another text file. I know this is straight forward and have figured out how to add multiple text lines to a file but am not sure how to add entire files. My current code is below and I would greatly appreciate any help. <?php $myFile = "textfile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "567\n"; fwrite($fh, $stringData); $stringData = "1293\n"; fwrite($fh, $stringData); fclose($fh); ?>
-
[SOLVED] str_replace and then array_sum ??? in script
trauch replied to trauch's topic in PHP Coding Help
I got it to work. <?php $lines = file('textfile.txt'); $lines = str_replace(' ','',$lines); echo 'sum($lines) = ' . array_sum($lines) . "\n"; $fp = fopen('total.txt', 'w'); $sum = array_sum($lines); fwrite($fp, $sum); fclose($fp); ?> -
Im trying to create a stepped php script. Below is an example of the contents of the text file (simple line of numbers) that Im trying to add and write to a new file. The problem is there are spaces in place of commas signifying the thousandths place - any number in the string over 999 incudes a space (ie 1 000) 540 460 1 007 2 000 3 000 4 001 When I use array_sum to add these numbers the total is "1010" when it should be 11,008. This is because it array_sum is only adding the numbers before the space. To address this, I've added the str_replace command before the sum command to remove the spaces. Unfortunately, this does not seem to work. Im not sure if this is because I added the str_replace incorrectly or because the array_sum occurs before the str_replace command happens. The script that Im working with is below. I would appreciate any help. <?php $string = "string to replace all spaces"; $string = str_replace(" ","",$string); $lines = file('textfile.txt/'); echo "sum($lines) = " . array_sum($lines) . "\n"; $lines = file('textfile.txt'); $sum = array_sum($lines); $fp = fopen('total.txt', 'w'); fwrite($fp, $sum); fclose($fp); ?>
-
Here is an example of the contents of the text file that Im trying to add and write to a new file. Please note that any number in the string over 999 incudes a space (ie 1 000) 1 007 2 000 3 000 4 001 When I use array_sum to add these numbers the total is "10" when it should be 10,008. I've added the str_replace command before the sum command but it does not seem to work. The script that Im working with is below <?php $string = "string to replace all spaces"; $string = str_replace(" ","",$string); $lines = file('textfile.txt/'); echo "sum($lines) = " . array_sum($lines) . "\n"; $lines = file('textfile.txt'); $sum = array_sum($lines); $fp = fopen('total.txt', 'w'); fwrite($fp, $sum); fclose($fp); ?>
-
I put together a script that that will allow for the adding of a string of numbers in a txt file. However, I've got a problem since the numbers have spaces in place of commas (ie 2 345 instead of 2,345). This causes an obvious problem when adding. I am therefore trying to remove the spaces. I considered the trim commands but they dont address spaces in the middle of numbers. I tried the str_replace command but it doesnt seem to work (or Im doing it wrong). Can someone help me add to this script to remove the spaces to thereby allow for the adding? <?php $lines = file('textfile.txt/'); echo "sum($lines) = " . array_sum($lines) . "\n"; $lines = file('textfile.txt'); $sum = array_sum($lines); $fp = fopen('total.txt', 'w'); fwrite($fp, $sum); fclose($fp); ?>
-
It turns out that file_put_contents is only php5 so Im a bit stuck. To say Im a newbie is being generous. I understand fopen and fwrite and used them in a basic open & write script but Im not sure how to associate the commands with this script. Can you help, AGAIN. Thanks a bunch