My project is a bistro menu, using PHP and MySQL. It prints out a pretty table of menu items, as long as they are in stock.
I made addRemove.php to add and remove items from the menu. It creates a button for each item, on or off. Items that are 'on' are in stock and displayed on the menu. When the user clicks a button, it will post the button's name back to the form, then the 'toggle' function performs an UPDATE on that item. If the button is on, it gets toggled to off. If it is off, it gets toggled to on.
Example: I have 3 items on the menu, Spaghetti, Pizza, and Cheeseburger. I run out of Spaghetti, so I go to addRemove.php.
Spaghetti, Pizza, and Cheeseburger load as buttons Pizza:[ON] Spaghetti:[ON] Cheeseburger:[ON]
I click the Spaghetti button, and the database correctly updates to: Pizza:[ON] Spaghetti:[OFF] Cheeseburger:[ON]
but the form shows: Pizza:[ON] Spaghetti:[ON] Cheeseburger:[ON]
then I click the Cheeseburger button, and the database is again correctly updated: Pizza:[ON] Spaghetti:[OFF] Cheeseburger:[OFF]
and the form now shows Pizza:[ON] Spaghetti:[OFF] Cheeseburger:[ON]
The form shown is one step behind the actual data. I think the problem is that the form is posted and reloaded before the 'toggle' UPDATE query is executed.
I don't know AJAX, is there a simple way to force a refresh after the UPDATE?
here is addRemove.php:
<?php include ("data2.php");
$keys = array_keys($_POST);
if(isset($keys[0])){
toggle($name[$keys[0]]);
}
?>
<form action="addRemove2.php" method="post">
<?php
global $name;
$c = count($name);
//$c = 2;
for($i=0;$i<$c;$i++){
$item = $name[$i];
if($inStock[$i]==1){
$onOff = "ON";$color = "blue";
}
else{
$onOff = "OFF";$color = "red";
}
echo "<label>$item</label>";
echo "<input type='submit' name='$i' value='$onOff' style='color:$color'>";
echo "<br />";
}
?>
</form>
<?php
function turnOn($item){
$dbc = mysqli_connect('localhost','root','','bigItaly')
or die('Error connecting to MySQL Server.');
$queryString = "UPDATE items SET inStock='1' WHERE name='$item'";
mysqli_query($dbc, $queryString);
mysqli_close($dbc);
}
function turnOff($item){
$dbc = mysqli_connect('localhost','root','','bigItaly')
or die('Error connecting to MySQL Server.');
$queryString = "UPDATE items SET inStock='1' WHERE name='$item'";
mysqli_query($dbc, $queryString);
mysqli_close($dbc);}
function getInStock($item){
global $name, $inStock;
$key = array_search($item, $name);
return $inStock[$key];
}
function toggle($item){
if (getInStock($item)=="1"){
turnOff($item);
}
else{
turnOn($item);
}
}
?>
</html>
here is the database schema.
INSERT INTO `items` (`id`, `name`, `unitPrice`, `inStock`, `category`) VALUES
(1, 'Fettucine Alfredo', 2, 1, 'Pastas'),
(2, 'Hawaiian', 3, 1, 'Pizzas'),
(3, 'Spaghetti', 3, 1, 'Pastas'),
(4, 'Supreme', 5, 1, 'Pizzas'),
(5, 'Deluxe Lasagna', 2, 1, 'Pastas'),
(6, 'Barbecue Chicken', 6, 1, 'Pizzas'),
(7, 'Guacomole Bacon', 3, 1, 'Burgers'),
(8, 'Swiss Mushroom', 2, 1, 'Burgers'),
(9, 'Breakfast Burger', 2, 1, 'Burgers');