I am making a cart, and I have 3 pages and 3 tables to insert products into this cart:
stock_mol_comp, stock_mol_trac, stock_mol_torcao
It worked perfectly when I only had compression page products. But now when I went to put the 3 pages to add to the cart does not work.
The cart works with $_SESSION
variables, this is the code:
<a class='fa fa-cart-plus' href='cart.php?add=carrinho&id=".$row['id_mol_comp']."'></a>
cart.php:
<?php
session_start();
if (!isset($_SESSION['itens'])){
$_SESSION['itens']=array();
}
if(isset($_GET['add'])&& $_GET['add']=="carrinho") {
$idproduto = $_GET['id'];
if(!isset($_SESSION['itens'][$idproduto])){
$_SESSION['itens'][$idproduto]=1;
}else{
$_SESSION['itens'][$idproduto]+=1;
}
}
header('location: carrinho.php');
?>
Then prints:
<?php
if (!isset($_SESSION['itens'])){
$_SESSION['itens']=array();
}
if (count($_SESSION['itens'])==0){
echo'Carrinho vazio <br><a href="mol_compressao.php">Adicionar Itens</a>';
}else{
echo"
<h2>Resumo de Compras</h2>
<br>
<div align='start'>
<div class='card mx-auto w-100'>
<div class='container-fluid''>
<div class='row align-items-end' style='height:40px;'>
<div class='col-md-2' style='text-align: center'><h5>REFERÊNCIA</h5></div>
<div class='col-md-3' style='text-align: center'><h5>QUANTIDADE</h5></div>
<div class='col-md-3' style='text-align: center'><h5>PREÇO UNITÁRIO</h5></div>
<div class='col-md-2' style='text-align: center'><h5>PREÇO TOTAL</h5></div>
</div>
</div>
<hr>";
include ("db.php");
$precototali=0;
foreach($_SESSION['itens'] as $idproduto => $quantidade){
$select_car = "SELECT * FROM stock_comp WHERE id_mol_comp=$idproduto";
$result = mysqli_query($con, $select_car);
$rows = array();
$select_car2 = "SELECT * FROM stock_torcao WHERE id_mol_torcao=$idproduto";
$result = mysqli_query($con, $select_car2);
$rows = array();
$select_car1 = "SELECT * FROM stock_trac WHERE id_mol_trac=$idproduto";
$result = mysqli_query($con, $select_car1);
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
$precototal=$row['preco']*$quantidade;
$precototali+=$precototal;
$precoiva=$precototali*1.23;
echo "
<div class='card-body'>
<div class='row align-items-center' style='height:0px;'>
<div class='col-md-2' style='text-align: center'>$row[referencia]</div>
<div class='col-md-3' style='text-align: center'><i style='font-size:18px' id='diminuir' class='fa'></i>  $quantidade  <i style='font-size:18px' id='aumentar'class='fa'></i></div>
<div class='col-md-3' style='text-align: center'> ".number_format($row['preco'],2,",",".")." €</div>
<div class='col-md-2' style='text-align: center'> ".number_format($precototal,2,",",".")." €</div>
<div class='col-md-2' style='text-align: center'><i class='fa fa-remove' style='font-size:24px'></i></div>
</div>
</div>";
}
}
echo "
<br>
<div class='col-md-11' style='text-align: end'><h4>Total(s/IVA): ".number_format($precototali,2,",",".")." €</h4></div>
<div class='col-md-11' style='text-align: end'><h2>Total(c/IVA): ".number_format($precoiva,2,",",".")." €</h2></div>
<br>";
}
echo"<hr>";
?>
But this way, just print the product with the id
of the last select
, in this case this select: (if I change the order the select will print what I put in last place)
$select_car1 = "SELECT * FROM stock_trac WHERE id_mol_trac=$idproduto";
$result = mysqli_query($con, $select_car1);
$rows = array();
For example:
When I have the cart empty and I'm going to add a product's page traction (which is the select that is in last) it adds correctly, but then when I'm going to add a product's page compression it increases the product's page traction.