Forum Moderators: coopster

Message Too Old, No Replies

Am i looking too hard?

a php math form

         

jimboharris21

8:26 am on Sep 2, 2008 (gmt 0)

10+ Year Member



i need a script that for example a customer will look at an online menu, and enter the quantity of each item they would like in a textbox, then at the bottom of the page display a total cost and the products ordered, and a copy to be emailed back to the resterant. would i have to use mysql or is there a way round it?

I can nail the email bit, just struggling with the math side, I send the form with $name which i use the name of the dish, then the $value is the quantity of dish required, but i would like the customer to see how much it will cost, Can anyone help?

Thanks

penders

9:52 am on Sep 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...would i have to use mysql or is there a way round it?

At the end of the day, you need to store your data (menu) somewhere. A MySQL database might be overkill for a relatively simple menu that doesn't change much. A plain text (CSV) file could suffice, or perhaps even a PHP array (quick and little overhead on the server if that is of concern). If your menu was small and rarely changed, it could even be hard coded into your HTML form. However, it could depend on how the menu is to be updated and how often. If a non-techy is to update the menu regularly and there are more than a few items then it might be wise to go for MySQL and a few Admin forms?! If you want to develop your webapp further in the future then may be MySQL is the way to go. But otherwise a non-MySQL solution could work better. It depends. :)

henry0

10:56 am on Sep 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you need a quick/direct result then you will need AJAX

anyway unless I miss your point
check all operator modes from the left hand side [us3.php.net]

do you write php? do you have something to share with us?

penders

12:15 pm on Sep 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



...just struggling with the math side, I send the form with $name which i use the name of the dish, then the $value is the quantity of dish required, but i would like the customer to see how much it will cost...

Behind the scenes you need to link the product with a unit cost then all the math is (as you are no doubt aware)...

cost_of_productA = unit_cost_of_productA * quantity_of_productA

I guess your problem is linking the product with a unit cost and a quantity?

A rough example of one way to do it... using a PHP array to hold the menu items...

<html><head><title>My Menu</title></head> 
<body>
<h1>My Menu</h1>
<?php
// All menu items defined
// product_id = 'Description,unit_price';
$MY_MENU = array(
'product1' => 'Beans on Toast,3.49',
'product2' => 'Boiled Egg,1.99',
'product3' => 'French Toast,3.00',
'product4' => 'Bacon and Egg,4.50'
);
// Form has been posted by user
if (isset($_POST['submitform'])) {
$grandtotal = 0;
$summary = '';
// Step through post vars and calc subtotals/grandtotal and generate summary
foreach ($_POST as $product_id => $quantity) {
// May be not all $_POST vars are for product quantites?
if (isset($MY_MENU[$product_id]) && ($quantity > 0)) {
$product_a = explode(',',$MY_MENU[$product_id]);
// Need to validate $quantity - ie. number within range
$subtotal = ($quantity * $product_a[1]);
$grandtotal += $subtotal;
// Build summary
$summary .= $quantity.' x '.$product_a[0].' (at $'.$product_a[1].' each) = $'.$subtotal."\n";
}
}
// Append grandtotal
$summary .= 'Total = $'.$grandtotal;
// Output to page...
echo 'Thanks...';
echo '<pre>'.$summary.'</pre>';
// Email output....
//mail($TO,'MENU',$summary);
}
// Display menu/form for first time
else {
$h = '<form action="'.$_SERVER[PHP_SELF].'" method="post">';
foreach($MY_MENU as $product_id => $product_detail) {
$product_a = explode(',',$product_detail);
$desc = $product_a[0].' (at $'.$product_a[1].' each)';
$h .= '<div>';
$h .= '<label for="'.$product_id.'">'.$desc.'</label>';
$h .= ' <input id="'.$product_id.'" name="'.$product_id.'" title="Quantity of '.$desc.'" value="0" size="3">';
$h .= '</div>';
}
$h .= '<div><input type="submit" id="submitform" name="submitform" value="Send"></div>';
$h .= '</form>';
echo $h;
}
?>
</body>
</html>

The product id is the key of the $MY_MENU array and also the name of the INPUT controls that the user submits the quantity in. The unit cost can then be looked up from the array when the form is processed.

jimboharris21

7:42 am on Sep 18, 2008 (gmt 0)

10+ Year Member


ok i am very confused, obviously I am new to PHP.

I have created the form and the inputs are as follows:

<input name="Soup" type="text" size="3" maxlength="2">

obviously there is a script to email the output soup:value

So here it is

$message = '';
foreach($_POST as $name => $value)
$message .= $name . ': ' . $value . "\n";

How do i add the costs then add them up?

can i do it as follows?

<input name="Soup" type="text" id="1.99" size="3" maxlength="2">
<input name="chicken" type="text" id="2.99" size="3" maxlength="2">
<input name="beef" type="text" id="3.99" size="3" maxlength="2">

then

$total = '';
foreach($id*&value)

I know this would not work but i did expain how new i was

Thanks in advance

penders

9:34 am on Sep 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your PHP needs to know about the unit cost of each food item. Only the name/value pairs of your form controls are passed back to your server-side PHP. The value of any other attributes are lost. (id="1.99" is not valid HTML anyway). So you shouldn't be storing the unit cost in your HTML, this needs to be handled by the PHP. Your PHP should really be driving this, even so far as creating your HTML form in the first place.

But anyway, to continue your example... you need to hold information about the unit cost of each food item. This could be done using a PHP array...

$unit_cost = array ( 
'Soup' => 1.99,
'chicken' => 3.49,
'beef' => 2.50
);

Then your script becomes...

$message = ''; $total = 0; 
foreach($_POST as $name => $value) {
$cost = $value * $unit_cost[$name];
$total += $cost;
$message .= $name . ' x ' . $value . ' = ' . $cost . "\n";
}
$message .= 'TOTAL: '.$total;