2
I’m trying to create a simple shopping cart (at first), where I have some fixed products in the database and I’m calling them, obviously, through the Fetchall();
Okay, I made a $_SESSION and as far as I can see, that’s okay, but my problem is that when I try to add several products in the same cart, that doesn’t 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=iddoproduto'. Here is the first point:
- I want to send the product to "cart.php" but instead, the product is going the following way: cart.php? add=cart&id=3 (<----- is product 3)
Also 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/>';
}
This code, I believe the problem is here, more specifically between checking the existence of session or showing the cart. When I click on "Add to cart" for each product there in index.php, it just leads to the page referring to 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 then of course, presents on the screen, the name of the product that is referenced ID. Good is this.
What I want is to click "add cart" in index.php and insert it below the last product (if any) in the "cart.php" reference. Am I clear? I hope yes, kkkk hate to be confused.
I hope you can help me, I added the code and I hope I’m not being too extensive on the issue. Well, this SESSION thing is new to me and I’m studying it now but I’m still answering a lot of questions, to those who can help me.
NOTE: I saw numerous other questions regarding this problem but either they had no answers or it wasn’t exactly what I needed to know. Every case is a case. If you can help me, I will be able to help those who still have no answer or you yourselves can add there the answers through what you suggested to me here.
Grateful! Att. :D
Do
session_start();
at the top of the PHP pages– Marcelo Shiniti Uchimura
Why didn’t you use {} in your foreach??
– Dudut
@Marcelouchimura is already brother ^^
– Lougans de Matos
@Dudut pq I am a beast.... '----------' I did not see that..... I DID NOT SEE THAT '-' kkkkkkkkkkkkkk our bro vlw, it was just that kkkkkkkkkkkkkk
– Lougans de Matos
I hope I helped. I could contribute back by voting on my answer?
– Dudut