Jump to content

Pizza Order Calculation Trouble


Nexus9988

Recommended Posts

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

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

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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.