Nexus9988 Posted February 27, 2011 Share Posted February 27, 2011 I wrote some code that asks the user to order some pizzas and all my out put is working great except I cant seem to calculate the total properly I attached my file if anyone wants to take a look for me but what I want to happen is kinda like this calcprice = ( (size of pizza + topping) * quanity ) + delivery charge thanks to anyone who cal help me out =] [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/229061-pizza-order-calculation-trouble/ Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 The variables you try to calculate with are strings, not numbers. You need to extract the numbers first. Try something like this on all the $_POST you are calculating with: <?php preg_match("/(\d+(\.\d+)?)/", $_POST['size'], $size); preg_match("/(\d+(\.\d+)?)/", $_POST['amount'], $amount); $total = $size[0] * $amount[0]; ?> I got the regexp from http://stackoverflow.com/questions/944400/extract-floating-point-numbers-from-a-string-in-php Link to comment https://forums.phpfreaks.com/topic/229061-pizza-order-calculation-trouble/#findComment-1180525 Share on other sites More sharing options...
Nexus9988 Posted February 27, 2011 Author Share Posted February 27, 2011 The variables you try to calculate with are strings, not numbers. You need to extract the numbers first. Try something like this on all the $_POST you are calculating with: <?php preg_match("/(\d+(\.\d+)?)/", $_POST['size'], $size); preg_match("/(\d+(\.\d+)?)/", $_POST['amount'], $amount); $total = $size[0] * $amount[0]; ?> I got the regexp from http://stackoverflow.com/questions/944400/extract-floating-point-numbers-from-a-string-in-php Thanks for the help looks like i got everything sorta working better I think I have an issue with the delivery part when its free. This is what I got done on it. preg_match("/(\d+(\.\d+)?)/", $_POST['size'], $size); preg_match("/(\d+(\.\d+)?)/", $_POST['topping'], $topping); preg_match("/(\d+(\.\d+)?)/", $_POST['delivery'], $delivery); $calcprice = (( $size[0] + $topping[0] ) * $_POST['quantity'] ) + $delivery[0]; UPDATE: So i found out its taking the first number in the sting and converting it into the number so it takes the "6" from the 6-8 weeks - Free. Think i have to write six to eight weeks - $0.00 to get the right number. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/229061-pizza-order-calculation-trouble/#findComment-1180539 Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 or you do <?php if (substr_count($_POST['delivery'], "Free") > 0) { $delivery[0] = 0; } else { preg_match("/(\d+(\.\d+)?)/", $_POST['delivery'], $delivery); } ?> Link to comment https://forums.phpfreaks.com/topic/229061-pizza-order-calculation-trouble/#findComment-1180548 Share on other sites More sharing options...
flolam Posted February 27, 2011 Share Posted February 27, 2011 Sry, posted to wrong topic Link to comment https://forums.phpfreaks.com/topic/229061-pizza-order-calculation-trouble/#findComment-1180563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.