Vote count:
0
I am developing an online shopping system and everything is working as I want, only except for one thing. I am not able to get the quantity reported for certain product. I'm trying to include it in the SESSION of each product. I've tried using foreach but without success. I would like greatly that someone examine the logic of my code and help me to achieve my goal. I'll put the relevant parts of my code to be more objective.
First, I have the page products.php and in this page the user can select the products:
$sql = "SELECT id_product, name_product, price_product FROM products
ORDER BY id_product";
$stmt = $connection->prepare($sql);
$stmt->execute();
$num = $stmt->rowCount();
if($num>0)
{
echo "<table>";
echo " <tr>";
echo " <th>NAME</th>";
echo " <th>PRICE</th>";
echo " <th>QUANTITY</th>";
echo " </tr>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
echo " <tr>";
echo " <td><div class='id-product' style='display: none'>{$id_product}</div>";
echo " <div class='name-product'>{$name_product}</div></td>";
echo " <td>R${$price_product}</td>";
echo " <td>";
echo " <form class='add'>";
echo " <input type='number' name='quantity' value='1' min='1' max='20'/>";
echo " <button type='submit'>Add product</button>";
echo " </form>";
echo " </td>";
echo " </tr>";
}
echo "</table>";
And now, to proccess the data I use jQuery:
<script>
$(document).ready(function()
{
$('.add').on('submit', function()
{
var id_product = $(this).closest('tr').find('.id-product').text();
var name_product = $(this).closest('tr').find('.name-product').text();
var quantity = $(this).closest('tr').find('input').val();
window.location.href = "add.php?id_product=" + id_product + "&name_product=" + name_product + "&quantity=" + quantity;
return false;
});
});
</script>
As you can see, I set the variable quantity with the value present on the input.
In page add.php I create the SESSION using:
$_SESSION['cart'][$id_product] = $name_product;
header('Location: products.php?action=added&id_product=' . $id_product . '&name_product=' . $name_product);
Now, to show the products that exists in the SESSION, I have the page cart.php:
if(count($_SESSION['cart'])>0)
{
$ids = "";
foreach($_SESSION['cart'] as $id_product=>$value)
{
$ids = $ids . $id_product . ",";
}
$ids = rtrim($ids, ',');
echo "<table>";
echo "<tr>";
echo "<th>Product name</th>";
echo "<th>Price</th>";
echo "</tr>";
$sql = "SELECT id_product, name_product, price_product from products WHERE id_product IN ({$ids}) ORDER BY name_product";
$stmt = $connection->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
echo "<tr>";
echo "<td>{$name_product}</td>";
echo "<td>R${$price_product}</td>";
echo "<td>THE QUANTITY NEEDS GOES HERE</td>";
echo "<td>";
echo "<a href='remove.php?id_product={$id_product}&name_product={$name_product}'>Remove</a>";
echo "</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><b>Total</b></td>";
echo "<td>$</td>"; //Here I need the sum of all prices with the quantities
echo "<td>";
echo "<a href='checkout.php'>Submit</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
else
{
echo "<p>Your cart is empty.</p>";
}
I do not know how to add the quantity of each product in the SESSION and then display it in the cart page. I would be grateful for any help.
Storing the quantity of each product related to it ID in a SESSION
Aucun commentaire:
Enregistrer un commentaire