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
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

Link to comment
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

 

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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