How do I get the difference between two numbers?

I’m working on showing a message telling users how much they need to spend to qualify for free shipping. This gets the free shipping level:

htmldisplay( $country['currency1'].round( $freeship[0]['value']*$country['exchange'],2 ) )

while this gets the basket total:

$total

I tried to get the difference by using these two but neither work

This gives me the total in the basket (i.e. $2):

htmldisplay( $country['currency1'].round( $total-$freeship[0]['value']*$country['exchange'],2 ) ) 

This gives the minus the total in the basket (i.e. -$2):

htmldisplay( $country['currency1'].round( $freeship[0]['value']-$total*$country['exchange'],2 ) )

Is the $total value stored in the same currency as the $freeship[0][‘value’] value? If so, wouldn’t it be something like

htmldisplay( $country['currency1'].round( ($freeship[0]['value']-$total)*$country['exchange'],2 ) )

probably surrounded with some logic that doesn’t carry on calculating how much they need to spend once their basket value is higher than the free shipping threshold? I’m assuming you mean you want to show how much more they need to spend to get free shipping, in addition to their current basket value, i.e. the difference between the free shipping level and their current basket value.

Your results imply that $freeship[0][‘value’] contains zero at this point.

Yep they’re both in the same currency but when I tried that it said I needed to spend -$2 (if I’ve got $2 in the basket then it should be saying spend $23 more).

Post the values of $freeship[0][‘value’] and $country[‘exchange’] please. You did note the additional brackets in my code?

Hmm

I just used

var_dump($freelevel[0]['value']);
	  var_dump($country['exchange']);

and that gave me : NULL float(1) although $country[‘exchange’] is just a number that gets the currency symbol

The whole code is:

$query="select max( value ) as value from freeship is null or voucher=".dbstr( $code )." )";
  $freeship=dbselect( $query,"dbShop" );
  if( is_array( $freeship ) ) {
    if( $freeship[0]['value'] ) {
      $MESSAGE[]="You have qualified for free shipping";
      $query="select * from freeship where no_preempt=0 and deleted=0 and value=".dbstr( round( $freeship[0]['value'],2 ) )." and ( voucher is null or voucher=".dbstr( $code )." )";
      $result=dbselect( $query,"dbShop" );
      if( is_array( $result ) ) {
        foreach( $result as $key=>$value ) {
          $change_shipping[$value['id']]['product_id']=$value['product_id'];
        }
      }
    } else {
      $MESSAGE[]="Spend  ".htmldisplay( $country['currency1'].round( ($freeship[0]['value']-$total)*$country['exchange'],2 ) ) ." to qualify for free shipping";
	  var_dump($freeship[0]['value']);
	  var_dump($country['exchange']);
    }
  }

I added this to the else statement and it now works :slight_smile:

Just thought I’d post it incase anybody else had a similar problem.

$query="select min( value ) as value from freeship where no_pre=0 and deleted=0 and value>=".dbstr( round( $total,2 ) )." and ( voucher is null or voucher=".dbstr( $code )." )";
  $freeatlevel=dbselect( $query,"dbShop" );

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.