Jump to content

Adding and Subtracting within pages.


LuigiLA

Recommended Posts

Hello everyone,

 

I am not a php programmer, but I would like to solve a problem regarding math with PHP. I was looking around on the web on how to add (in this case digits) using PHP.

I found out that adding two strings using a . I can then echo out the results; so far so good.

My question however is as follow. How do I add up information from multiple pages? Let's say on one page (let's call it weekly.php) I have my weekly expenses of $300, and on the second page (let's call it monthly.php) I have my monthly expenses of $1400. I would like the result (weekly + monthly) to be displayed (echo'ed out) on the third page (let's call it totalamount.php).

 

I don't want to take advantage of anyone's time, so I am not asking for the entire solution (code) unless you want to  ::), but I would be just happy to be pointed in the right direction, I will do the rest on my own.

 

Thank you very much for your time.

 

Cheers,

 

Luigi

Link to comment
Share on other sites

how are you getting these numbers(The 300 and 1400)? from a: form, database, other?

 

I am getting the number from just plain text. Imagine it's like in Excel, when using SUM=(A1+A2). By manually change A1 the SUM will adjust accordingly.

There is no database attached to this.

Link to comment
Share on other sites

To elaborate on the above post, are these values hard coded into an html page? Or are they being submitted through a form?

 

Yes, hard coded. I manually change the number on Page 1 and 2. I just want to be able to get the sum of them on the third page automatically using php.

Link to comment
Share on other sites

You could use PHP to scrape both pages for the number, but that's a slow process that you can avoid.

 

Are you interested in doing this the right way, which will be a little more work, but make updating easier?

 

Would you prefer the quick n dirty way, and not have to change the way those other two pages are set up?

Link to comment
Share on other sites

You could use PHP to scrape both pages for the number, but that's a slow process that you can avoid.

 

Are you interested in doing this the right way, which will be a little more work, but make updating easier?

 

Would you prefer the quick n dirty way, and not have to change the way those other two pages are set up?

 

If there is a right away, I wouldn't mind using it. I am open to suggestions. It's not something urgent, so there is no rush to finish it up quickly.

Thanks!

Link to comment
Share on other sites

Okay, well, we're going to use a text file to store our information. This is generally called a flat-file database.

 

Create a new text file, and type the two numbers in it, separated by commas. To make things as simple as possible, put the file in the same directory as the 3 files that need to use the information.

 

numbers.txt

500,1200

 

You can then use PHP to open this text file, grab it's contents and put them into a variable, split that variable up using the comma, and add, or simply output the data.

 

samplefile.php

<?php

// This will attempt to grab the contents of 'numbers.txt' and put it into $raw_data
$raw_data = file_get_contents( 'numbers.txt' );

// If file_get_contents() fails, it will return FALSE.
// We use an 'if' conditional statment to check if this has happened
if( $raw_data == FALSE ) {
// If it failed, we abort the script and output an error message
die( 'Could not get data from text file!' );
}

// If the scrip has made it this far, we know file_get_contents() grabbed the data
// successfully. We can now use the 'explode' function to split it up by the comma
$data = explode( ',', $raw_data );

// $data now holds an array, where $data[0] is the first value, $data[1] is the second value
// and $data[2] would be the third value, if we had it, and so on.

// We'll now output the data we have
echo 'The first value is '.$data[0].'<br>';
echo 'The second value is '.$data[1].'<br>';
echo 'The sum of the values is '.($data[0]+$data[1]).'<br>';
echo 'We can also use array functions to automatically add them: '.array_sum($data);

?>

 

So you'd include similar code in all 3 files, and either echo the value you want or add them up and echo them.

 

To make it even more streamlined, you could create a single file that populates $data, and use include to simply add the code to each of your files, then echo out the information you need.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.