if when

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

if when

Post by meandrew »

Hi all I am adding some 'basic' code to a system that adds a shipping value based on the price to the total cart $total which will then return a $grand_total

My problem is that I am unsure if I am doing this correctly, if the total amount is less than 45 then 2.50 is added but if it is 45 and over 0 gets added, this is what I have so far but oit isn't passing a value through

Code: Select all

/* Delivery Costs. */
	$delivery_costs = "1.50";
    $delivery_costs2 = "0.00";
	



/* Grand Total. */
	$grand_total = ($total + $delivery_costs);



if ($total > "45.00") {
    print "$grand_total = ($total + $delivery_costs)";
} elseif ($total == "45.00") {
    print "$grand_total = ($total + $delivery_costs)";
} else {
    print "$grand_total = ($total + $delivery_costs2)";
}
any help would be greatly appreciated

Andrew
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

do this:

Code: Select all

/* Delivery Costs. */ 
   $delivery_costs = "1.50"; 
    $delivery_costs2 = "0.00"; 
    
/* Grand Total. */ 
   $grand_total = $total + $delivery_costs; 


//the less than or equal to removes one line of code
if ($total <= "45.00") { 
    echo "$grand_total = ($total + $delivery_costs)"; 
}
else { 
    echo "The grand total is: ".$grand_total; 
}
I have tidied up your code for you, but the only problem was that you were using the print statement to display variables... This is fine except you have to use single quotes and not double ones. To save yourself worrying about silly things like this just use the echo statement instead, it does the same without the rubbish.
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

I must be doing soemthing else wrong because this is outputting:

1.5 = ( + 1.50)

the delivery added to the total is 1.50 but I don't think is being added becuse of this code. :(

Andrew
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

I just noticed that when you are declaring your variables you are setting them up as strings... i.e. putting them in quotes. Try this:

Code: Select all

/* Delivery Costs. */ 
   $delivery_costs = 1.50; 
    $delivery_costs2 = 0.00; 
    
/* Grand Total. */ 
   $grand_total = $total + $delivery_costs; 


//the less than or equal to removes one line of code 
if ($total <= "45.00") { 
    echo "$grand_total = ".$total + $delivery_costs; 
} 
else { 
    echo "The grand total is: ".$grand_total; 
}
Last edited by m@ndio on Fri Jul 04, 2003 6:47 am, edited 1 time in total.
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

Code: Select all

<?
  $delivery_costs = 1.50;

  if($total<=45.00){
    $grand_total = $total + $delivery_costs;
  }
  else{
    $grand_total = $total;
  }
  echo $grand_total;
?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Personally I would do something along the lines of:

Code: Select all

<?php

if ($total <= 45.00) {
	$delivery_costs = 1.50;
} else {
	$delivery_costs = 0.00;
}

$grand_total = $total + $delivery_costs;

echo 'Grand Total = $'.number_format($grand_total, 2);

?>
Where does $total come from - are you sure it is available to be used? Have you done something like:

Code: Select all

echo '$total = '.$total;
to check it?

Mac
meandrew
Forum Commoner
Posts: 49
Joined: Mon Feb 24, 2003 1:03 pm

Post by meandrew »

thanks for the help this is working now... and I am a happy bunny :):)


there is another part to this where I want text to display that if the order is over £45 the delivery is free so I'll give that a go now :)

Andrew
Post Reply