I am trying to create a canteen order calculator without using MySQL (project spec, not in my control). The idea is that a user enters some values into the HTML table and then receives the calculated total cost of their order. Most other ways I've tried resulted in fatal errors. This solution is only marginally better, always outputting a cost of $0. Does anyone know why it's doing that, and how I could fix it?
<?php
if
(
isset($_POST['rolls'])
or isset($_POST['pies'])
or isset($_POST['crosses'])
or isset($_POST['chickens'])
or isset($_POST['pastas'])
or isset($_POST['milks'])
or isset($_POST['juices'])
or isset($_POST['waters'])
)
{
$finalcost=floatval(0);
$rolls=0.00;
$pies=0.00;
$crosses=0.00;
$pastas=0.00;
$chickens=0.00;
$milks=0.00;
$juices=0.00;
$waters=0.00;
function pricefixer ($a,$b)
{
switch($b) # find which post to look at.
{
case 'rolls':
$c=$_POST['rolls'];
break;
case 'crosses':
$c=$_POST['crosses'];
break;
case 'chickens':
$c=$_POST['chickens'];
break;
case 'pastas':
$c=$_POST['pastas'];
break;
case 'juices':
$c=$_POST['juices'];
break;
case 'pies':
$c=$_POST['pies'];
break;
case 'milks':
$c=$_POST['milks'];
break;
case 'waters':
$c=$_POST['waters'];
break;
default:
$c=0;
break;
}
if($c>0)
{
$a+=$c;
$a==floatval($a); #Decimal/float prices need decimal/float multipliers.
}
}
function priceadder ($a, $b)
{
for ($x=0; $x<$a; $x++)
{
$finalcost+=$b;
}
}
#simplifes calculation code later
$order=1; #used to check if user has ordered
#See function pricefixer above
pricefixer($rolls, 'rolls');
pricefixer($pies, 'pies');
pricefixer($chickens, 'chickens');
pricefixer($pastas, 'pastas');
pricefixer($crosses, 'crosses');
pricefixer($milks, 'milks');
pricefixer($waters, 'waters');
pricefixer($juices, 'juices');
#Calculate cost
priceadder($rolls, 4.50);
priceadder($pies, 2.50);
priceadder($crosses, 5.00);
priceadder($chickens, 7.00);
priceadder($pastas, 6.30);
priceadder($milks, 3.50);
priceadder($juices, 3.00);
priceadder($waters, 2.50);
}
else
{
$order=0;
}
?>
<html>
<head>
<!-- For when stylehseet is made: <link rel="stylesheet" href="[stylesheet_name_here].css" -->
<meta charset="utf-8">
<title>Canteen Menu</title>
</head>
<body>
<table width=<?php echo $wide; ?> height=<?php echo $tall; ?> border="1" align="center">
<tr>
<td colspan="6" align="center" height="15%"> <h2> <b> Hello <?php echo "$given"." "."$surname" ?>, welcome to Narrabundah College online shop. </h2> </b>
<br> You are signed in as:<?php echo "$user" ?>. <br> Today's date is <?php echo"$date"; ?>. </td>
</tr>
<tr>
<td rowspan="4" align="center" width="15%">
<a href="canteen_menu.php"> Canteen Data </a> <br>
<a href="customers_menu.php"> Customer Data </a> <br>
<a href="stationery_menu.php"> Stationery Data </a> <br>
<a href="logout.php"> Log Out </a> <br>
</td>
</tr>
<tr>
<td rowspan="4" colspan="4" align="center">
<!-- Table for easy fo alignment and readability for user -->
<?php
if ($order==0)
{
?>
<table border="1">
<tr>
<td colspan="2">
<h3> Welcome To The Canteen </h3>
</td>
</tr>
<tr>
<td>
Items Currentely Avalible:
<ul align="left">
<li> Sausage Roll ($4.50) </li>
<li> Beef and Mushroom Pie ($6.50) </li>
<li> Almond Crossaint ($5.00) </li>
<li> Satay Chicken and Rice [conatins peanuts] ($7.00)</li>
<li> Spaghetti Bolognese ($6.30) </li>
<li> Chocolate Milk ($3.50) </li>
<li> Sparkling Raspberry Juice ($3.00) </li>
<li> PUMP Water ($2.50) </li>
</ul>
</td>
<td>
Order Quantities: <br> <br>
<form action="<?php $self ?>" method="post">
<ul style="list-style-type:none;"> <!-- Removes bullets, list assists in formatting -->
<li> <input type="number" name="rolls"> </li>
<li> <input type="number" name="pies"> </li>
<li> <input type="number" name="crosses"> </li>
<li> <input type="number" name="chickens"> </li>
<li> <input type="number" name="pastas"> </li>
<li><input type="number" name="milks"> </li>
<li> <input type="number" name="juices"> </li>
<li> <input type="number" name="waters"> </li>
</ul>
<input type="submit" value="Submit" align="center">
</form>
</tr>
</table>
</td>
<?php
}
if ($order==1)
{
echo 'That order comes to $'.$finalcost.'.';
}
?>
</tr>
</table>
</body>
</html>
Here are some of the issues
Variables defined outside a function are not automatically available inside it.
The pricefixer function tries to modify $a (passed by value), but that will not affect the original variables ($rolls, $pies, etc.) outside the function.
Ditto for $finalcost
in priceadder. This is why you get 0.
Use an array ($itemPrices
) to map item names to their costs, making it easier to manage and scale. Iterate (loop) over the $_POST data, matching the price array so you can calculate the total directly-
Make sure the submitted quantities are numeric by casting them to int
Use the $_SERVER['REQUEST_METHOD'] === 'POST' to verify the form submission and wrap the $_SERVER['PHP_SELF']
in htmlspecialchars() for security
Other things: $wide, $tall, $given, $surname, $user are not defined in your PHP - this will result in "Undefined variable" errors.
"Beef and Mushroom Pie" have two different prices
Suggested code - more DRY (your switch is inefficient for example)
(disclaimer: I'm a JS expert with SOME PHP knowledge)
<?php
// Missing in your original code and would cause errors.
$wide = "100%";
$tall = "auto";
$given = "Customer";
$surname = ""; //
$user = "Guest";
$date = date("Y-m-d"); // Current date
$finalCost = 0.00;
$orderPlaced = false;
// Define item prices
$itemPrices = [
'rolls' => 4.50,
'pies' => 6.50, // !
'crosses' => 5.00,
'chickens' => 7.00,
'pastas' => 6.30,
'milks' => 3.50,
'juices' => 3.00,
'waters' => 2.50
];
// A check to see if any order quantities have been submitted is simpler than multiple 'isset'
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
foreach ($itemPrices as $itemName => $price) {
if (isset($_POST[$itemName]) && is_numeric($_POST[$itemName])) {
$quantity = (int)$_POST[$itemName]; // cast to (int)
if ($quantity > 0) {
$finalCost += ($quantity * $price);
$orderPlaced = true; // We have at least one order
}
}
}
}
?>
<table width="<?= $wide; ?>" height="<?= $tall; ?>" border="1" align="center">
<tr>
<td colspan="6" align="center" height="15%">
<h2><b>Hello <?= "$given $surname," ?>, welcome to Narrabundah College online shop.</b></h2>
<br> You are signed in as:<?= "$user" ?>. <br> Today's date is <?= "$date"; ?>.
</td>
</tr>
<tr>
<td rowspan="4" align="center" width="15%">
<a href="canteen_menu.php">Canteen Data</a> <br>
<a href="customers_menu.php">Customer Data</a> <br>
<a href="stationery_menu.php">Stationery Data</a> <br>
<a href="logout.php">Log Out</a> <br>
</td>
</tr>
<tr>
<td rowspan="4" colspan="4" align="center">
<?php if (!$orderPlaced) : ?>
<table border="1">
<tr>
<td colspan="2">
<h3>Welcome To The Canteen</h3>
</td>
</tr>
<tr>
<td>
Items Currently Available:
<ul align="left">
<li>Sausage Roll ($4.50)</li>
<li>Beef and Mushroom Pie ($6.50)</li>
<li>Almond Croissant ($5.00)</li>
<li>Satay Chicken and Rice [contains peanuts] ($7.00)</li>
<li>Spaghetti Bolognese ($6.30)</li>
<li>Chocolate Milk ($3.50)</li>
<li>Sparkling Raspberry Juice ($3.00)</li>
<li>PUMP Water ($2.50)</li>
</ul>
</td>
<td>
Order Quantities: <br><br>
<form action="<?= htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<ul style="list-style-type:none;">
<li><input type="number" name="rolls" value="0" min="0"></li>
<li><input type="number" name="pies" value="0" min="0"></li>
<li><input type="number" name="crosses" value="0" min="0"></li>
<li><input type="number" name="chickens" value="0" min="0"></li>
<li><input type="number" name="pastas" value="0" min="0"></li>
<li><input type="number" name="milks" value="0" min="0"></li>
<li><input type="number" name="juices" value="0" min="0"></li>
<li><input type="number" name="waters" value="0" min="0"></li>
</ul>
<input type="submit" value="Submit" align="center">
</form>
</td>
</tr>
</table>
<?php else : ?>
That order comes to $<?= number_format($finalCost, 2); ?>.
<?php endif; ?>
</td>
</tr>
</table>