Forum Moderators: coopster

Message Too Old, No Replies

Adding up data in a file write !

         

php_ds

10:03 am on Apr 24, 2010 (gmt 0)

10+ Year Member



I am making up a financial planning page. I am trying to subtract outgoings and add incomings from a balance and then write this to a file.

How would I add up the data from incomings and outgoings ?

Thanks for your help

Readie

10:25 am on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World php_ds.

Umm, I'm not really 100% certain what you're asking. I'll go with what I think you're asking, so apologies if this sounds patronising. Assuming they're int/floats, then you could just do this:

$incoming = '478.25';
$outgoing = '129.36';
$net = ($incoming - $outgoing);
echo $net;

php_ds

12:54 pm on Apr 24, 2010 (gmt 0)

10+ Year Member



Sorry I wasn't very specific ..
Basically , when I register a new user I write 3 personal files. One for incomings , one for outgoings and one for balance.
Once the user as inputed a incomings, i want to be able to add the content of that file and then be able to output it. The same with outgoings. Then I can deduct that from the balance.
I can't work out how I would add the data in the file and output it.
Any ideas ?
Thanks

Readie

4:21 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you could show me a snippet (change the values to pseudonyms if it's sensitive data) of one of these files, I'd be able to offer much more targetted assistance here.

php_ds

5:49 pm on Apr 24, 2010 (gmt 0)

10+ Year Member



I am inputting using a post from a form. Then the code to write to a file is:

$amount = $_POST['amount'] ;

$myFile = "$name balance .txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$amount\n";
fwrite($fh, $stringData);
fclose($fh);

Then outputting the file by using the code :
$myFile = "$name incomings.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;

With $name being a passed variable.

Readie

6:29 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, I'm again not 100% sure what you need here, but the way I see it you're asking for one of two things, so I'll just detail them both :)

- Adding up all lines of data in the entire file
$file = 'balance.txt';
$vals = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$count = count($vals);
$tot = 0;
for($i = 0; $i < $count; $i++) {
$vals[$i] = trim($vals[$i]);
$tot += $vals[$i];
}
echo $tot;

- Getting the latest values for income, outgoings and balance, updating balance accordingly
$bal_file = 'balance.txt';
$inc_file = 'income.txt';
$out_file = 'expenses.txt';

$bal = end(file($bal_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
$inc = end(file($inc_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
$out = end(file($out_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));

$bal += ($inc - $out);
echo $bal;