jakebur01 Posted July 4, 2007 Share Posted July 4, 2007 hey my little shipping calculation has been working fine recently. But, I noticed that when you carts total is 3,534.99 it returns 5.99 for some odd reason. Could someone help me perfect this? function calculate_shipping_cost() { // return 20.00; $t_number = number_format($_SESSION['total_price'],2); if ($t_number >= 0.01 and $t_number <= 14.99) { return 5.99; } else if ($t_number >= 15.00 and $t_number <= 29.99) { return 8.99; } else if ($t_number >= 30.00 and $t_number <= 49.99) { return 10.99; } else if ($t_number >= 50.00 and $t_number <= 74.99) { return 12.99; } else if ($t_number >= 75.00 and $t_number <= 99.99) { return 14.99; } else if ($t_number >= 100.00 and $t_number <= 149.99) { return 16.99; } else if ($t_number >= 150.00 and $t_number <= 199.99) { return 18.99; } else if ($t_number >= 200.00 and $t_number <= 299.99) { return 21.99; } else if ($t_number >= 300.00 and $t_number <= 5000.00) { return 28.99; } else { return 10.00; } } Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/ Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 Try adding this into your function at the top: global $t_number; That's telling PHP that you want to use that variable inside the function instead of treating it as a new variable. By default variables are local to functions. Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289526 Share on other sites More sharing options...
grlayouts Posted July 4, 2007 Share Posted July 4, 2007 it could be the , ie 10000 instead of 10,000 may be throughing it off. are you using a shopping cart or can you provide the rest of the code? Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289527 Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 Sorry, my bad! Don't add that line, I didn't read it properly. What I would do though is use echo to display the contents of $t_number so you can see what value it is. Then you'll know how its being treated. Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289531 Share on other sites More sharing options...
jakebur01 Posted July 4, 2007 Author Share Posted July 4, 2007 it's a shopping cart. I think your right though. I will see if i can't find something that will remove unwanted charactors. Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289532 Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 $t_number=str_replace(',','',$t_number); That will remove all commas. Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289534 Share on other sites More sharing options...
Yesideez Posted July 4, 2007 Share Posted July 4, 2007 Or you can use this: $t_number= str_replace(',','',number_format($_SESSION['total_price'],2)); Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289535 Share on other sites More sharing options...
jakebur01 Posted July 4, 2007 Author Share Posted July 4, 2007 That was it. It works really well now. Thank you all. Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289537 Share on other sites More sharing options...
jakebur01 Posted July 4, 2007 Author Share Posted July 4, 2007 I was having similar issues when trying to do a 10 percent discount on all of my items. I believe the comma was the problem in that situation as well. Link to comment https://forums.phpfreaks.com/topic/58396-solved-if-statements/#findComment-289541 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.