<section class="contenedor">
            <div class="contenedor-items">      
                <?php 
                    $query= "SELECT * FROM produc";
                    $result= mysqli_query($conexion,$query);



                    while($row = mysqli_fetch_array($result)) {?>
                    
                
                    
                    <form method="POST" class="kk" action="tienda.php?id=<?=$row['id'] ?>">
               
                            <div class="item">  
                                <img src="img/<?=$row['img']?>" alt="" class="img-item">
                                <span class="titulo-item"><?=$row['producto']?></span>
                                <span class="descripcion " ></span>
                                <span class="precio-item">$<?=number_format($row ['precio'],2)?></span>
                                <input type="hidden" name="producto" value="<?= $row ['producto']?>">
                                <input type="hidden" name="imagen" value="<?= $row ['img']?>">
                                <input type="hidden" name="precio" value="<?= $row ['precio']?>">
                                <input type="number" name="quantity" value="1" class="form-control">
                                <input type="submit" name="anadealcarrito" class="btn btn-primary bnt-block my-2 btn-lg " value="Añadir">
                                <a href="productos.php?mas=<?php echo $row["id"]?>" name="mas" class="btn btn-primary bnt-block my-2 btn-lg"  value="ver mas">Ver Más</a>
                       </div> 

                    
                      </form>
                 
                
                     <?php }


                     ?> 
                            </div>
                        </div>
                     </div>
                    
                        <?php
                        $total = 0;

                        $output = "
                            <div class='carrito' id='carrito'>
                                <div class='header-carrito'>
                                    <h2>Tu Carrito</h2>
                                    <span class='aquis'><i class='fa-solid fa-x' onclick='hacerInVisibleCarrito()'></i></span>
                                </div>
                    
                                <div class='carrito-items'>
                        "; 
                    
                        if (!empty($_SESSION['cart'])) {
                            foreach ($_SESSION['cart'] as $key => $value) {
                                $output .= "
                                    <div class='carrito-item'>
                                        <img src='img/" . $value['img'] . "' width='80px' alt=''>
                                        <div class='carrito-item-detalles'>
                                            <span class='carrito-item-titulo'>" . $value['id'] . "</span>
                                            <span class='carrito-item-titulo'>" . $value['producto'] . "</span>
                                            <div class='selector-cantidad'></div>
                                            <span class='carrito-item-precio'>" . $value['quantity']. ' x  ' . $value['precio'] . '$' . "</span>
                                        </div>
                                        <form method='post' action='tienda.php'>
                                            <input type='hidden' name='remove_id' value='" . $value['id'] . "'>
                                            <button type='submit' class='btn-eliminar' name='eliminar_item'>
                                                <i class='fa-solid fa-trash'></i>
                                            </button>
                                        </form>
                                    </div>
                                ";
                    
                                $total = $total + $value['quantity'] * $value['precio'];
                            }
                        }
                    
                        $output .= "
                                </div>
                                <div class='carrito-total'>
                                    <div class='fila'>
                                        <strong>Tu Total</strong>
                                        <span class='carrito-precio-total'>$" . number_format($total, 2) . "</span>
                                    </div>
                                    <form method='post' action='tienda.php'>
                                        <button type='submit' class='btn-pagar' name='borrar_todo'>Comprar<i class='fa-solid fa-bag-shopping'> </i></button>
                                    </form>
                                </div>
                            </div>
                        ";
                    
                        echo $output;
                    
                        if (isset($_POST['eliminar_item'])) {
                            $remove_id = $_POST['remove_id'];
                            foreach ($_SESSION['cart'] as $key => $value) {
                                if ($value['id'] == $remove_id) {
                                    unset($_SESSION['cart'][$key]);
                                }
                            }
                        }
                    
                        if (isset($_POST['borrar_todo'])) {
                            unset($_SESSION['cart']);
                        }
                        ?>

                      

          </div>      
    </section>
    <?php
    
    if(isset($_GET['action'])){
          

        if($_GET['action']=="clearall"){
            unset($_SESSION['cart']);
        }

        if($_GET['action']=="remove"){
            foreach($_SESSION['cart'] as $key => $value){
                if($value['id']==$_GET['id']){
                    unset($_SESSION['cart'][$key]);

                }
            }

        }
    }
    
    ?>

         
</section>