Jump to content

PHP maths


esoteric

Recommended Posts

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';

 

 

 

Link to comment
Share on other sites

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

 

:confused:

 

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>';
/////////////////////////////

Link to comment
Share on other sites

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);

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.