I have this assignment and i have been working on it all weekend.
Here is the scope of the assignment,
- Products are stored in a multidimensional array
- Display the products in a table
- Update the product quantity using '+' and '-' button
- If quantity > 0, display a '-' button to update quantity as required.
- Checkout button to display -- quantities > 0, total prices of products whose quantities are greater than 0.
I am stuck at updating the table because each time i click on a different '+' button on the table, the other table rows reverse back to their original state and i don't want that. I want their values to remain after clicking on another '+' button on the table.
- I updated product 1 quantity then
- I updated product 2 quantity and the updated quantity and subtotal price in product 1 is set back to its initial array values.
The logic i want to use is "update whole multidimensional array after each click on various '+' and '-' button and display the 'updated' array back into the table. But I have problem implementing this logic or is their another logic to go about this??
Here is my code so far for the assignment.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
$ShowForm = True;
$items = array(
"Product 1" => array
(
"Qty" => 0,
"Item" => "Product 1",
"Unit Price" => 5,
"Subtotal" => 0
),
"Product 2" => array
(
"Qty" => 0,
"Item" => "Product 2",
"Unit Price" => 2.3,
"Subtotal" => 0
),
"Product 3" => array
(
"Qty" => 0,
"Item" => "Product 3",
"Unit Price" => 3.54,
"Subtotal" => 0
),
"Product 4" => array
(
"Qty" => 0,
"Item" => "Product 4",
"Unit Price" => 1.67,
"Subtotal" => 0
),
"Product 5" => array
(
"Qty" => 0,
"Item" => "Product 5",
"Unit Price" => 3.4,
"Subtotal" => 0
)
);
?>
<h3>Update the products below as required.<br>Then you can click on the checkout button below.</h3>
<form method="POST" action= "<?php $_SERVER['PHP_SELF']; ?>" >
<?php
echo '<table border = 1 id=\'products\'>';
echo '<th>Qty</th>';
echo '<th>Item</th>';
echo '<th>Unit Price</th>';
echo '<th>Subtotal</th>';
$i = count($items);
foreach ($items as $key => $value) {
echo'<tr class=\'' . $key . '\'>';
$i--;
if ($key === "Product 1") {
$product1 = array();
foreach ($value as $dim => &$num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add4'])) {
$newQty = ($_POST['qty4']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty4\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove4\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
$value['Qty'] = $newQty;
}
if (ISSET($_POST['remove4'])) {
$newQty = ($_POST['qty4']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty4\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove4\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 2") {
foreach ($value as $dim => &$num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add3'])) {
$newQty = ($_POST['qty3']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty3\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove3\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
$value['Qty'] = $newQty;
}
if (ISSET($_POST['remove3'])) {
$newQty = ($_POST['qty3']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty3\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove3\'> </td>';
}
//
}
}elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 3") {
foreach ($value as $dim => $num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add2'])) {
$newQty = ($_POST['qty2']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty2\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove2\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
}
if (ISSET($_POST['remove2'])) {
$newQty = ($_POST['qty2']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty2\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove2\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 4") {
foreach ($value as $dim => $num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add1'])) {
$newQty = ($_POST['qty1']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty1\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove1\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
}
if (ISSET($_POST['remove1'])) {
$newQty = ($_POST['qty1']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty1\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove1\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
if ($key === "Product 5") {
foreach ($value as $dim => $num) {
if ($dim === "Qty") {
echo '<td><input type=\'submit\' value=\'+\' name=\'add' . $i . '\'> ' . $num;
if (ISSET($_POST['add0'])) {
$newQty = ($_POST['qty0']) + 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty0\'>';
echo '<input type=\'submit\' value=\'-\' name=\'remove0\'> </td>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
}
if (ISSET($_POST['remove0'])) {
$newQty = ($_POST['qty0']) - 1;
echo $newQty;
//echo '<input type=\'submit\' value=\'+\' name=\'add'.$i.'\'> '.$newQty;
echo '<input type=\'hidden\' value=\'' . $newQty . '\' name=\'qty0\'>';
$value['Subtotal'] = $newQty * $value['Unit Price'];
if ($newQty > 0) {
echo '<input type=\'submit\' value=\'-\' name=\'remove0\'> </td>';
}
}
} elseif ($dim === "Subtotal") {
echo '<td>'.$value['Subtotal'].'</td>';// . '</td>';
}else {
echo '<td>' . $num . '</td>';
}
}
}
echo'</tr>';
}
?>
<p><input type="submit" value="Check Out" name="checkout">
</form>
<?php
if (ISSET($_POST['checkout'])) {
echo $newQty;
}
?>
</body>
</html>
P.S: I'm not asking for code handout, just explanations and code snippets (examples) is fine. Thanks.