LuigiLA Posted December 10, 2011 Share Posted December 10, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/ Share on other sites More sharing options...
The Little Guy Posted December 10, 2011 Share Posted December 10, 2011 how are you getting these numbers(The 300 and 1400)? from a: form, database, other? Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296589 Share on other sites More sharing options...
xyph Posted December 10, 2011 Share Posted December 10, 2011 To elaborate on the above post, are these values hard coded into an html page? Or are they being submitted through a form? Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296592 Share on other sites More sharing options...
LuigiLA Posted December 10, 2011 Author Share Posted December 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296606 Share on other sites More sharing options...
LuigiLA Posted December 10, 2011 Author Share Posted December 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296607 Share on other sites More sharing options...
xyph Posted December 10, 2011 Share Posted December 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296616 Share on other sites More sharing options...
LuigiLA Posted December 10, 2011 Author Share Posted December 10, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296621 Share on other sites More sharing options...
xyph Posted December 10, 2011 Share Posted December 10, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296628 Share on other sites More sharing options...
LuigiLA Posted December 11, 2011 Author Share Posted December 11, 2011 xyph, I can't thank you enough for your time. This works perfectly for what I want to do. Thank you so much!!! Quote Link to comment https://forums.phpfreaks.com/topic/252892-adding-and-subtracting-within-pages/#findComment-1296809 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.