savagenoob Posted August 23, 2010 Share Posted August 23, 2010 OK, I think I have confused this code and it is not calculating correctly. $ckifdue = mysql_query("SELECT * FROM accounting WHERE ClientID = '$clientid'") or die(mysql_error()); while($ckdue =mysql_fetch_array($ckifdue)){ if($ckdue['AmountDue'] == "0"){ $_SESSION['MONEYOWED'] = "0.00"; } else { $_SESSION['MONEYOWED'] = $_SESSION['MONEYOWED'] + $ckdue['AmountDue']; } if($ckdue['Amount1RS'] == "Balance Due"){ $totcol = $ckdue['TotalCollected']; echo " total collected:" . $totcol; $monpaid = $_SESSION['MONEYPAID']; echo "money paid in:" . $monpaid; $_SESSION['MONEYPAID'] = $totcol + $monpaid; echo "session money paid:" . $_SESSION['MONEYPAID']; } } $moneypaid = $_SESSION['MONEYPAID']; $moneyowed = $_SESSION['MONEYOWED']; echo "money paid:" . $moneypaid; echo " money owed:" . $moneyowed; $moneyowed = $moneyowed - $moneypaid; unset($_SESSION['MONEYOWED']); unset($_SESSION['MONEYPAID']); The echos look like: total collected:100.00money paid in:200session money paid:300 total collected:100.00money paid in:300session money paid:400money paid:400 money owed:0.00 There should be $100 due, $200 paid, so a balance of -100. There is 2 balance due reciepts, this is just not calclating right in the sessions. Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/ Share on other sites More sharing options...
samshel Posted August 23, 2010 Share Posted August 23, 2010 Try this: //Bring this statement out of while $_SESSION['MONEYOWED'] = "0.00"; while($ckdue =mysql_fetch_array($ckifdue)){ //remove if else, even if amount due is 0, it will add 0 $_SESSION['MONEYOWED'] = $_SESSION['MONEYOWED'] + $ckdue['AmountDue']; Keep the remaining code as it is. Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/#findComment-1102787 Share on other sites More sharing options...
savagenoob Posted August 23, 2010 Author Share Posted August 23, 2010 OK, but doesnt fix the math problem here, the session seems to be starting at 200 then adding 100 2 more times for some reason making the amount paid 400 when it should just be 200. Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/#findComment-1102827 Share on other sites More sharing options...
samshel Posted August 23, 2010 Share Posted August 23, 2010 please post sample records in the table and expected output in simple words. Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/#findComment-1102834 Share on other sites More sharing options...
savagenoob Posted August 23, 2010 Author Share Posted August 23, 2010 The echos look like: total collected:100.00money paid in:200session money paid:300 total collected:100.00money paid in:300session money paid:400money paid:400 money owed:0.00 There should be $100 due, $200 paid, so a balance of -100. There is 2 balance due reciepts, this is just not calclating right in the sessions. I thought I did Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/#findComment-1102845 Share on other sites More sharing options...
samshel Posted August 23, 2010 Share Posted August 23, 2010 I mean the actual records in your table: Like AmountDue Amount1RS TotalCollected 100 Balance Due 0 100 100 Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/#findComment-1102858 Share on other sites More sharing options...
savagenoob Posted August 24, 2010 Author Share Posted August 24, 2010 It would be like... Amount1RS TotalCollected AmountDue New Business 200(or w/e) 100 Balance Due 100 Balance Due 100 I just did 2 balance dues that exceeded the amount due on this particular client for testing purposes but wouldnt happen normally. Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/#findComment-1102991 Share on other sites More sharing options...
savagenoob Posted August 24, 2010 Author Share Posted August 24, 2010 I knew it was simple, I had the elseif statement in the first loop which went through its own loop causing it to add to much, just moved it down a bracket... while($ckdue =mysql_fetch_array($ckifdue)) { if($ckdue['Amount1RS'] == "New Business") { $_SESSION['MONEYOWED'] = $_SESSION['MONEYOWED'] + $ckdue['AmountDue']; } } if($ckdue['Amount1RS'] == "Balance Due"){ $totcol = $ckdue['TotalCollected']; $sesscol = $_SESSION['MONEYPAID']; $_SESSION['MONEYPAID'] = $totcol + $sesscol; } $moneypaid = $_SESSION['MONEYPAID']; $moneyowed = $_SESSION['MONEYOWED']; $moneyowed = $moneyowed - $moneypaid; unset($_SESSION['MONEYOWED']); unset($_SESSION['MONEYPAID']); Quote Link to comment https://forums.phpfreaks.com/topic/211525-if-statement-with-session-variable-problem/#findComment-1103261 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.