ririe44 Posted March 12, 2009 Share Posted March 12, 2009 Hey, how would you go about putting this together... database: abcdef 151820351375 I want to add a, and f (which would give me 90), then I want to subtract the rest of them from that amount (90-(18+20+35+13))=90-86=4 Do I have to create a variable for every single value in my table and then do something like this? $total=$a+$f-($b+$c+$d+$e) ?? My problem will be that the structure in my table may change in the future... but my a and f columns will be constant, so I'd like to maybe have a variable for the sum of a and f, then have it subtract all other values in the table... Any ideas? Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/ Share on other sites More sharing options...
Mchl Posted March 12, 2009 Share Posted March 12, 2009 Why not do this in SQL query? Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783073 Share on other sites More sharing options...
Maq Posted March 12, 2009 Share Posted March 12, 2009 Make an associative array: $letters = array("a" => 15, "b" => 18, "c" => 20, "d" => 35, "e" => 13, "f" => 75); foreach($letters AS $letter => $value) { $$letter = $value; } $total=$a+$f-($b+$c+$d+$e); echo $total; ?> EDIT: didn't see this was in a database... A query would be so much easier Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783077 Share on other sites More sharing options...
ririe44 Posted March 12, 2009 Author Share Posted March 12, 2009 Yes, it will have to be done in sql query, I'm just not sure where to start... Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783084 Share on other sites More sharing options...
Mchl Posted March 12, 2009 Share Posted March 12, 2009 SELECT a,b,c,d,e,f, (a+f-(b+c+d+e)) AS sum FROM table WHERE ? Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783090 Share on other sites More sharing options...
Maq Posted March 12, 2009 Share Posted March 12, 2009 Use a function, something like: function doMath($equation) { $sql = "SELECT a, b, c, d, e, f, $equation AS sum FROM table"; echo $sql; } $theMath = "(a+f-(b+c+d+e))"; doMath($theMath); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783107 Share on other sites More sharing options...
ririe44 Posted March 12, 2009 Author Share Posted March 12, 2009 Okay, thinking through it some more, I'm thinking a table like this would be better: categoryamount a15 b18 c20 d35 e13 f75 so, with the new table structure, could you do something like this:? $total = mysql_query("SELECT 'amount' from `$tbl_name2` AS sum WHERE 'category' = 'a', 'category' = 'f'"); $total2 = mysql_query("SELECT 'amount' from `$tbl_name2` AS sum WHERE 'category' = 'b', 'category' = 'c', 'category' = 'd', 'category' = 'e'"); $grand_total = $total+$total2 Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783125 Share on other sites More sharing options...
Mchl Posted March 12, 2009 Share Posted March 12, 2009 More like this: $result1 = mysql_fetch_row(mysql_query("SELECT SUM('amount') AS sum from `$tbl_name2` WHERE 'category' IN('a','f') ")); $result2 = mysql_fetch_row(mysql_query("SELECT SUM('amount') AS sum from `$tbl_name2` WHERE 'category' IN ('b','c','d','e') ")); $sum1 = $result1['sum']; $sum2 = $result2['sum']; $grand_total = $sum1 - $sum2; Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783134 Share on other sites More sharing options...
ririe44 Posted March 12, 2009 Author Share Posted March 12, 2009 Hmmm... I'm just getting 0 as my result. $result1 = mysql_fetch_row(mysql_query("SELECT 'amount' AS sum FROM `$tbl_name2` WHERE 'category' IN ('a','k','l') ")); $result2 = mysql_fetch_row(mysql_query("SELECT 'amount' AS sum FROM `$tbl_name2` WHERE 'category' IN ('b','c','d','e','f','g','h','i','j') ")); $sum1 = $result1['sum']; $sum2 = $result2['sum']; $grand_total = $sum1 - $sum2; Isn't the "IN" in the select from where mean 'or'? I guess I don't understand how this would total all the amounts specified? Also, what is happening with the $result['sum']? Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783172 Share on other sites More sharing options...
Mchl Posted March 12, 2009 Share Posted March 12, 2009 Should be SELECT SUM('amount') AS sum ... if you select only 'amount' you're not summing up the rows. Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783183 Share on other sites More sharing options...
Daniel0 Posted March 12, 2009 Share Posted March 12, 2009 And even worse, doing SELECT 'amount' AS sum will select the string literal amount, not the field value amount. That's also why you get zero. You have two strings amount. Converting them to an integer (which is implicitly done using the arithmetic subtraction operator) gives you int 1. So 1-1=0. Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783198 Share on other sites More sharing options...
ririe44 Posted March 12, 2009 Author Share Posted March 12, 2009 I'm still getting 0 with SUM('amount')... $result1 = mysql_fetch_row(mysql_query("SELECT SUM('amount') AS sum FROM `$tbl_name2` WHERE 'category' IN ('a','k','l') ")); $result2 = mysql_fetch_row(mysql_query("SELECT SUM('amount') AS sum FROM `$tbl_name2` WHERE 'category' IN ('b','c','d','e','f','g','h','i','j') ")); $sum1 = $result1['sum']; $sum2 = $result2['sum']; $grand_total = $sum1 - $sum2; is the $result1['sum'] coming from the 'AS sum'? Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783201 Share on other sites More sharing options...
Daniel0 Posted March 12, 2009 Share Posted March 12, 2009 You need to check up on data types. It's one of the fundamental concepts of computer science. You are still using a string literal. You need to replace 'amount' with either amount or `amount`. The regular quotes won't work because these signify strings in SQL. Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783202 Share on other sites More sharing options...
Mchl Posted March 12, 2009 Share Posted March 12, 2009 Huh... missed that... Same with 'category' Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783203 Share on other sites More sharing options...
Daniel0 Posted March 12, 2009 Share Posted March 12, 2009 Oh yeah that too. It's like checking if the string category equals the string a which is obviously false. Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783205 Share on other sites More sharing options...
ririe44 Posted March 12, 2009 Author Share Posted March 12, 2009 So... the code below should return a value besides 0? Because it still is. When I print $result1 I get "Array", when I print $sum1 I get nothing... I still don't understand $result1['sum']... it seems that this is where my error is coming from. $result1 = mysql_fetch_row(mysql_query("SELECT SUM(`amount`) AS sum FROM `$tbl_name2` WHERE `category` IN ('a','k','l') ")); $result2 = mysql_fetch_row(mysql_query("SELECT SUM(`amount`) AS sum FROM `$tbl_name2` WHERE `category` IN ('b','c','d','e','f','g','h','i','j') ")); $sum1 = $result1['sum']; $sum2 = $result2['sum']; $grand_total = $sum1 - $sum2; Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783219 Share on other sites More sharing options...
Daniel0 Posted March 12, 2009 Share Posted March 12, 2009 Try to run the queries directly on the database and check your results. Quote Link to comment https://forums.phpfreaks.com/topic/149132-simple-math/#findComment-783232 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.