John Davidson

html - (PHP) Shopping Cart

0 comments
Message:


I am trying to create a shopping cart with PHP, but, once the user leaves the cart area, all of the products disappear. That's what I'm trying to do:


  <?php foreach($almofadas as $almofadas):?>
<form action="cart.php" method="GET">
<div class="base col-6 col-sm-4 col-md-5 col-lg-4 col-xl-4">
<div class="card">
<img src="uploads/<?php echo $almofadas['imagem']; ?>" alt="">
<div class="content-c">
<div class="row-p">
<div class="details">

<span><?php echo $almofadas['pnome'] ; ?></span>
</div>
<div class="price">R$ <?php echo $almofadas['preço'];?> </div>
</div>
<input type="hidden" name="id" value="<?php echo $almofadas['p_id']?>">

<div style="margin-top: 10px;">
<div style="margin-bottom: 5px;"><button class="buttons-1" data-toggle="modal" data-target="#myModal">Detalhes</button></div>
<div><button class="buttons-2" type="submit">Adicionar ao Carrinho</a> </button></div>
</div>
</div>
</div>
</div>
</form>

<?php endforeach; ?>


Now the cart system:


<?php
session_start();


require_once 'conn.php';



$_SESSION['id'] = $_GET['id'];
$result_pedido = "SELECT * FROM tb_produtos WHERE p_id = '{$_SESSION['id']}'";
$resultado_pedido = mysqli_query($conn, $result_pedido);
$pedidos = mysqli_fetch_all($resultado_pedido, MYSQLI_ASSOC);

?>

Here I can only add one product and, I can't save it into a $_SESSION, having said that the product disapears once iI leave the cart.


<?php foreach($pedidos as $pedidos):?>
<tr>
<td>
<div class="cart-img">
<img src="uploads/<?php echo $pedidos['imagem'];?>" width="125px">
</div>
</td>
<td>
<div class="cart-model">
<?php echo $pedidos['pnome'] ; ?>
</div>
</td>
<td>
<div class="cart-quantity">
<input class="i-quantity" type="number" value="1">
</div>
</td>
<td>
<div class="cart-price">
R$<?php echo $pedidos['preço'] ; ?>
</div>
</td>
</tr>
</tbody>
</table>
<?php endforeach; ?>


My Answer:
To create a shopping cart that persists even after the user leaves the cart area, you can use PHP sessions to store the cart items. Here's a basic example of how you can achieve this:

1. Start a session at the beginning of your PHP script:

php
session_start();


2. When a user adds a product to the cart, store the product information in the session variable:

php
if(isset($_POST['add_to_cart'])) {
$product_id = $_POST['product_id'];
$product_name = $_POST['product_name'];
$product_price = $_POST['product_price'];

$_SESSION['cart'][$product_id] = array(
'name' => $product_name,
'price' => $product_price
);
}


3. To display the cart items, loop through the session variable:

php
if(isset($_SESSION['cart'])) {
foreach($_SESSION['cart'] as $product_id => $product) {
echo $product['name'] . ' - $' . $product['price'] . '
';
}
}


4. To remove a product from the cart, unset the corresponding session variable:

php
if(isset($_POST['remove_from_cart'])) {
$product_id = $_POST['product_id'];
unset($_SESSION['cart'][$product_id]);
}


By using PHP sessions, the cart items will be stored on the server and will persist even after the user leaves the cart area. Remember to handle session expiration and clearing the cart when the user completes the purchase.

Rate this post

5 of 5 based on 1478 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us