Note : <input type="hidden"> is an important HTML form element which can hold some data to be sent to the PHP script. But they are not visible to the HTML viewers.
foodMenu.html
<form action="foodMenu.php" method="post">
<div>
<p>Lunch</p>
<p>
<span><input type="hidden" value="Food Item 1 " name="itemName[]">Food Item 1
<input type="hidden" value="100" name="itemCost[]">100/=</span><br>
<input type="textbox" value="0" name="itemQTY[]">
<input type="button" value="Select" >
</p>
<p>
<span><input type="hidden" value="Food Item 2" name="itemName[]">Food Item 2
<input type="hidden" value="150" name="itemCost[]">150/=</span><br>
<input type="textbox" value="0" name="itemQTY[]">
<input type="button" value="Select">
</p>
<p>
<span><input type="hidden" value="Food Item 3" name="itemName[]">Food Item 3
<input type="hidden" value="150" name="itemCost[]">150/=</span><br>
<input type="textbox" value="0" name="itemQTY[]">
<input type="button" value="Select" >
</p>
<input type="submit" value="Submit" >
</div>
foodMenu.php
<?php
$itemName=$_POST['itemName']; //Grab the posted item names from the html form
$itemQTY=$_POST['itemQTY']; //Grab the posted item QTY from the html form
$itemPrice=$_POST['itemCost']; //Grab the posted item prices from the html form
$grandTotal=0;
echo('<table>');
echo('<tr><td>Meal List</td></tr>');
echo('<tr><td>Items</td><td>QTY</td><td>Cost</td></tr>');
for($count=0;$count<count($itemName);$count++){ //loop to print stored data in the arrays
if($itemQTY[$count]>0){
echo("<tr>");
echo('<td>'.$itemName[$count].'</td><td>'.$itemQTY[$count].'</td><td>'.subTotal($itemPrice,$itemQTY,$count));
echo("</tr>");
}
}
echo('<tr>');
echo('<td>'.'</td><td>'.totalQTY($itemQTY).'</td><td>'.$grandTotal.'</td>');
echo('</tr>');
echo('</table>');
//function to calculate sub totals and grand totals
function subTotal($itemPrice,$itemQTY,$count){
global $grandTotal; //refer to above global variable at the begining
$subTotal[$count]=$itemPrice[$count]*$itemQTY[$count];
$grandTotal=$grandTotal+$subTotal[$count];
return $subTotal[$count];
}
//function to calculate the total number of the QTYs
function totalQTY($itemQTY){
return array_sum($itemQTY);
}
?>
No comments:
Post a Comment