I'm trying to create a simple shopping cart (at the beginning), where I have some fixed products in the database and I'm obviously calling them through FetchAll ();
Okay, I did a $ _SESSION and as far as I can see, this is ok, but my problem is that when I try to add multiple products in the same cart, this does not happen. See the code below:
<?php
$conection = new PDO('mysql:host=localhost;dbname=meusprodutos', 'admin', 'nuttertools');
$query = $conection->prepare("SELECT * FROM produtos");
$query->execute();
$result = $query->fetchAll();
foreach($result as $produto){
echo 'Nome do produtos: '.$produto['nome'].'<br/>';
echo 'Qtde do produtos: '.$produto['qtde'].'<br/>';
echo 'Preço do produtos: '.number_format($produto['preco'], 2, ",",".").'<br/>';
echo '<a href="carrinho.php?add=carrinho&id='.$produto['id'].'">Adicionar ao carrinho</a><br/><br/>';
}
In the tag there is a link to 'cart.php? add = cart & id = iddproduct'. Here's the first point:
- I want to send the product to the "cart.php" but instead the product is going to the following path: cart.php? add = cart & id = 3 (< ---- is the product 3)
In addition there is another problem. See the code below:
<?php
/* Inicializa uma sessão */
session_start();
/* Verifica se já existe uma sessão ativa */
if(!isset($_SESSION['itens'])){
$_SESSION['itens'] = array();
}
/* Inicializa uma sessão */
if(isset($_GET['add']) && $_GET['add'] == 'carrinho'){
$idProduto = $_GET['id'];
if(!isset($_SESSION['itens'][$idProduto])){
$_SESSION['itens'][$idProduto] = 1;
}else{
$_SESSION['itens'][$idProduto] += 1;
}
}
/* Mostrar carrinho de compras */
if(count($_SESSION['itens']) == 0){
echo 'Carrinho Vazio<br/><a href="index.php">Adicionar Itens</a>';
}else{
$conection = new PDO('mysql:host=localhost;dbname=meusprodutos', 'admin', 'nuttertools');
foreach($_SESSION['itens'] as $idProduto => $qtde)
$query = $conection->prepare("SELECT * FROM produtos WHERE id=?");
$query->bindParam(1, $idProduto);
$query->execute();
$produtos = $query->fetchAll();
echo
$produtos[0]["nome"].'<br/>';
}
That code, I believe the problem is here, more specifically between checking the existence of a session or showing the cart. When I click on "Add to Cart" for each product there in index.php, it just takes you to the page for your link:
localhost / Responsive / cart / cart.php? add = cart & id = 1 localhost / Responsive / cart / cart.php? add = cart & id = 2 localhost / Responsive / cart / cart.php? add = cart & id = 3
and there of course, it displays on the screen the product name that is ID reference. Well that's it.
What I want is to click on "add cart" in index.php and it is inserted below the last product (if any) in the reference "cart.php". Was I clear? I hope so, kkkk hate to be confused.
I hope you can help me, I've added the code and I hope I'm not being too long in the matter. Well, this SESSION thing is new to me and I'm studying it right now but I'm still expressing a lot of doubts to those who can help me.
NOTE: I saw numerous other questions regarding this problem but either they had no answers or it was not exactly what I needed to know. Each case is different. If you can help me, I will be able to help those who do not yet have an answer or you can add the answers there by what you have suggested to me here.
Thank you! Att.: D