How do i stop update button if my quantity drops to 0?
In a cart page, user can update the quantity of an item(default when added is 1) so how do i stop the user from updating the quantity OR stop the updating process if the new quantity will drop the current quantity to 0 or below?
This is the code:
function showCart() {
global $db;
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',', $cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[] = '<form action="cart.php?action=update" method="post" id="cart">';
$output[] = '<table border = "1" class ="table">';
$output[] = '<tr height = "40">';
$output[] = '<td width="168" align="center"> </td>';
$output[] = '<td width="168" align="center">Title</td>';
$output[] = '<td width="168" align="center">Author</td>';
$output[] = '<td width="168" align="ce开发者_如何学Gonter">Price</td>';
$output[] = '<td width="168" align="center">Quantity</td>';
$output[] = '<td width="168" align="center">Total Amount</td>';
$output[] = '<tr >';
foreach ($contents as $id => $qty) {
$sql = 'SELECT * FROM books WHERE id = ' . $id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
$output[] = '<tr height ="40" >';
$output[] = '<td width ="140" align="center"><a href="cart.php?action=delete&id=' . $id . '" class="r"><img alt="" src="image/x.png" width = "30px" height="30px"></a></td>';
$output[] = '<td width ="140" align="center">' . $name . '</td>';
$output[] = '<td width ="140" align="center">' . $author . '</td>';
$output[] = '<td width ="140" align="center">SGD' . $price . '</td>';
$output[] = '<td width ="140" align="center"><input type="text" name="qty' . $id . '" value="' . $qty . '" size="3" maxlength="3" /></td>';
$output[] = '<td width ="140" align="center" >SGD' . ($price * $qty) . '</td>';
$total += $price * $qty;
$output[] = '</tr>';
}
$output[] = '<tr>';
$output[] = '<td>';
$output[] = '<center>';
$output[] = '<div class = "update_button"><button type="submit" >Update cart</button></div>';
$output[] = '</center>';
$output[] = '</td>';
$output[] = '<td colspan ="5" align="right">';
$output[] = '<p>Grand total: <strong>SGD:' . $total . '</strong> </p>';
$output[] = '</td>';
$output[] = '</tr>';
$output[] = '</table>';
$output[] = '</form>';
$output[] = '<br>';
} else {
$output[] = '<center>';
$output[] = '<br><h3>You shopping cart is empty.</h3><br>';
$output[] = '</center>';
}
return join('', $output);
}
Are You the author of shown code? I assume that You are trying to modify someone else's code and thus I explain bit more throughly:
The code that You have shown does not update anything. The PHP code You have shown takes the shopping cart content from $_SESSION parameter and inserts it into $contents array.
$cart = $_SESSION['cart'];
$items = explode(',', $cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$contents is now an array.
After that SQL query is made for each product in $contents, to find out product details, such as name, author, etc.
$sql = 'SELECT * FROM books WHERE id = ' . $id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);
Then a HTML table is produced that contains each of the products and a field to change the quantity of product.
The $output variable contains the HTML table, but the HTML table is never outputted.
I assume that You have only shown us part of the code from a bigger system. To answer Your question, You should show us the PHP code that has to do with processing the POST request and updating the data.
EDIT: As seen from code
'<form action="cart.php?action=update" method="post" id="cart">';
the POST data is sent to file cart.php?action=update when You click "update" button. So You should show us some more code from cart.php.
I think you are updating table and decrease qty by 1 when someone purchase it..
when your qty hits to 0 in db table then dont put purchase button..
or dont fetch from table when qty is 0 or <0
Maintain the total number of Items available at any time in the Model (Model should have the current state of any item). Whenever user tries to add items to cart, check if the number of items user is trying to add is more than the number of items available, then give user an alert that these many items are not available.
精彩评论