esoteric Posted September 24, 2011 Share Posted September 24, 2011 Can anyone see what im doing wrong here. I have a variable called 'TOTAL' which is the total amount the order comes too. A variable called 'customer_credit' which is the amount of credit a user has on there account, this is posted by a hidden field on my form. My form contains an option as to whether or not a customer wants to use there available credit and if so set the variable of 'credit' to Y. If the 'credit' variable equals Y i want to subtract the amount of credit the user has ('customer_credit') from the total ('TOTAL'). My script; if ($credit == 'Y'){ $total_credit = $TOTAL - $customer_credit; $connection = mysql_connect("$host", "$user", "$pass") or die (mysql_error()); mysql_select_db("$db") or die (mysql_error()); mysql_query ("UPDATE users SET user_credit='".$total_credit."' WHERE id='".$customer_id."' ") or die (mysql_error()); } if ($credit == 'N'){ $total_credit_unchanged = ("Credit not used. You could have saved $customer_credit on this order!"); } For a test i set the credit on my account to '200.00', i then went to purchase an item which costs '25.00'. The invoice then printed as "invoice total - £-175", it also saves the users total credit to '-175'. How can i make it so the credit is subtracted from the total, then the remaining credit are saved to teh database whilst the new total is shown on the invoice? Sorry for the long and possibly complicated post, i tried to explain best i could. Appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/247783-php-maths/ Share on other sites More sharing options...
sunfighter Posted September 24, 2011 Share Posted September 24, 2011 $total_credit = $TOTAL - $customer_credit; is wrong. Use $total_credit = $customer_credit - $TOTAL; You should also have a check to make sure $TOTAL < $customer_credit; $customer_credit has to be larger than the amount spent ie. $TOTAL. Quote Link to comment https://forums.phpfreaks.com/topic/247783-php-maths/#findComment-1272397 Share on other sites More sharing options...
esoteric Posted September 24, 2011 Author Share Posted September 24, 2011 Ok i changed that line, done the same test as i did above but the invoice total is now saying "total invoice £175", so the - sign is gone but i how do i just show what the total with the credit subtracted is? using the example above again, if i take £25 (cost of the item) from £200 (amount of credit available) that should show the invoice total as £0 and the remaining credit on the account as £175. $total_credit should be the new total value shouldn't it? in this case £0? Quote Link to comment https://forums.phpfreaks.com/topic/247783-php-maths/#findComment-1272402 Share on other sites More sharing options...
WebStyles Posted September 24, 2011 Share Posted September 24, 2011 check if total credit is bigger (or equal) than total item costs. if so, subtract item costs from credit, and set invoice to 0. if($TOTAL <= $total_credit){ // is credit enough to pay bill? // YES $total_credit -= $TOTAL; // remove item cost from credit $TOTAL = 0; // set total cost to 0 for invoice }else{ // NO $TOTAL -= $total_credit; // use available credit $total_credit = 0; // } echo 'Invoice Total: '.$TOTAL.'<br>'; echo 'Remaining Credit: '.$total_credit.'<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/247783-php-maths/#findComment-1272423 Share on other sites More sharing options...
esoteric Posted September 24, 2011 Author Share Posted September 24, 2011 thanks for your reply. two things, what does '-=' mean? Also i tested it again but changed the credit on my account to £17.50, the item is still £25 and the result returned as; Total Invoice: £32.5 (should be £7.50?) Remaining Credit: £0 my script so far; ////////////////////////////// // check if credit used if ($credit == 'Y'){ $total_credit = $customer_credit - $TOTAL; $connection = mysql_connect("") or die (mysql_error()); mysql_select_db("") or die (mysql_error()); mysql_query ("UPDATE users SET user_credit='".$total_credit."' WHERE id='".$customer_id."' ") or die (mysql_error()); } if($TOTAL <= $total_credit){ // is credit enough to pay bill? // YES $total_credit -= $TOTAL; // remove item cost from credit $TOTAL = 0; // set total cost to 0 for invoice }else{ // NO $TOTAL -= $total_credit; // use available credit $total_credit = 0; // } // echo 'Invoice Total: '.$TOTAL.'<br>'; // echo 'Remaining Credit: '.$total_credit.'<br>'; ///////////////////////////// Quote Link to comment https://forums.phpfreaks.com/topic/247783-php-maths/#findComment-1272432 Share on other sites More sharing options...
WebStyles Posted September 24, 2011 Share Posted September 24, 2011 so basically, you set your credit to 17,50 and tried to purchase an item priced at 25,00. but you did this: (which is not part of the code I gave you) $total_credit = $customer_credit - $TOTAL; so your $total_credit just became -7,50 (17,50 - 25,00). Since your item was 25,00 and you owed 7,50 (from having a negative credit), of course your invoice is going to be 32,50. 1. remove that line of code, and replace all instances of $total_credit with $customer_credit in the code I gave you. or (just to make it simpler for now) 2. change that line to $total_credit = $customer_credit; for testing purposes and fix variable names later. BTW: $total_credit -= $TOTAL; is shorthand for $total_credit = ($total_credit - $TOTAL); Quote Link to comment https://forums.phpfreaks.com/topic/247783-php-maths/#findComment-1272433 Share on other sites More sharing options...
esoteric Posted September 24, 2011 Author Share Posted September 24, 2011 Oh, that makes sense. Thank you for explaining it. That seems to be working. Thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/247783-php-maths/#findComment-1272436 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.