Porque será que o objeto esta sobrescrevendo?
Detalhe: session_start() na primeira linha de todas as páginas.
Código (PHP):
Código (PHP):
Detalhe: session_start() na primeira linha de todas as páginas.
Código (PHP):
<h1 class="h1Centralizado">
Carrinho
</h1>
<?php
require_once "controlls/util/Conexao.php";
require_once "controlls/models/Produtos.php";
require_once "controlls/models/Carrinho.php";
require_once "controlls/daos/ProdutosDao.php";
require_once "controlls/daos/CarrinhoDao.php";
require_once "controlls/util/PhpUtil.php";
$connection = new Conexao();
$conexao = $connection->abreConexao();
$produtoDao = new ProdutosDao($conexao);
$phpUtil = new PhpUtil();
if(!isset($_SESSION["carrinho"])) {
$_SESSION["carrinho"] = serialize(new CarrinhoDao());
}
$CarrinhoDao = unserialize($_SESSION["carrinho"]);
?>
<?php
if(isset($_GET["acao"]) && $_GET["acao"] == "adicionar") {
$novoProduto = $produtoDao->pesquisaProdutoEdicao($_GET["idProduto"]);
if($novoProduto != null) {
$CarrinhoDao->insereProduto($novoProduto, 1);
}
//header("Location: ?");
}
?>
<?php
if(isset($_GET["acao"]) && $_GET["acao"] == "excluir") {
$CarrinhoDao->removeItem($_GET["indice"]);
//header("Location: ?");
}
?>
<?php
$mensagem = "Deseja excluir este Produto?";
$imagem = "<img src='_img/bloquear.png' height=30px title='Excluir Produto' />";
echo "<table style='width:800px'";
echo " <tr>";
echo " <th>Produto</th>";
echo " <th>Tamanho</th>";
echo " <th>Preço Unitário</th>";
echo " <th>Quantidade</th>";
echo " <th>Preço Total</th>";
echo " <th>Remover</th>";
echo " </tr>";
foreach ($CarrinhoDao->getCarrinho() as $k=>$produto) {
echo " <tr>";
echo " <td style='text-align:left'>".$produto[0]->getNome()."</td>";
echo " <td style='text-align:center'>".$produto[0]->getTamanho()."</td>";
echo " <td style='text-align:right'>".$phpUtil->formataMoeda($produto[0]->getPreco())."</td>";
echo " <td style='text-align:center'><input type='text' id='qtde' name='qtde' class='typetextpequeno' style='text-align:right' value='".$produto[1]."'></td>";
echo " <td style='text-align:right'>".$phpUtil->formataMoeda($produto[0]->getPreco() * $produto[1])."</td>";
echo " <td style='text-align:center'><a href='?acao=remove&indice=".$k."' onclick=\"return verifica('".$mensagem."')\" '>".$imagem."</a>";
echo " </tr>";
}
echo "</table>";
print "<pre>";
print_r($CarrinhoDao->getCarrinho());
print "</pre>";
?>
Mesmo fazendo:Carrinho
</h1>
<?php
require_once "controlls/util/Conexao.php";
require_once "controlls/models/Produtos.php";
require_once "controlls/models/Carrinho.php";
require_once "controlls/daos/ProdutosDao.php";
require_once "controlls/daos/CarrinhoDao.php";
require_once "controlls/util/PhpUtil.php";
$connection = new Conexao();
$conexao = $connection->abreConexao();
$produtoDao = new ProdutosDao($conexao);
$phpUtil = new PhpUtil();
if(!isset($_SESSION["carrinho"])) {
$_SESSION["carrinho"] = serialize(new CarrinhoDao());
}
$CarrinhoDao = unserialize($_SESSION["carrinho"]);
?>
<?php
if(isset($_GET["acao"]) && $_GET["acao"] == "adicionar") {
$novoProduto = $produtoDao->pesquisaProdutoEdicao($_GET["idProduto"]);
if($novoProduto != null) {
$CarrinhoDao->insereProduto($novoProduto, 1);
}
//header("Location: ?");
}
?>
<?php
if(isset($_GET["acao"]) && $_GET["acao"] == "excluir") {
$CarrinhoDao->removeItem($_GET["indice"]);
//header("Location: ?");
}
?>
<?php
$mensagem = "Deseja excluir este Produto?";
$imagem = "<img src='_img/bloquear.png' height=30px title='Excluir Produto' />";
echo "<table style='width:800px'";
echo " <tr>";
echo " <th>Produto</th>";
echo " <th>Tamanho</th>";
echo " <th>Preço Unitário</th>";
echo " <th>Quantidade</th>";
echo " <th>Preço Total</th>";
echo " <th>Remover</th>";
echo " </tr>";
foreach ($CarrinhoDao->getCarrinho() as $k=>$produto) {
echo " <tr>";
echo " <td style='text-align:left'>".$produto[0]->getNome()."</td>";
echo " <td style='text-align:center'>".$produto[0]->getTamanho()."</td>";
echo " <td style='text-align:right'>".$phpUtil->formataMoeda($produto[0]->getPreco())."</td>";
echo " <td style='text-align:center'><input type='text' id='qtde' name='qtde' class='typetextpequeno' style='text-align:right' value='".$produto[1]."'></td>";
echo " <td style='text-align:right'>".$phpUtil->formataMoeda($produto[0]->getPreco() * $produto[1])."</td>";
echo " <td style='text-align:center'><a href='?acao=remove&indice=".$k."' onclick=\"return verifica('".$mensagem."')\" '>".$imagem."</a>";
echo " </tr>";
}
echo "</table>";
print "<pre>";
print_r($CarrinhoDao->getCarrinho());
print "</pre>";
?>
Código (PHP):
if(!isset($_SESSION["carrinho"])) {
$_SESSION["carrinho"] = serialize(new CarrinhoDao());
}
$CarrinhoDao = unserialize($_SESSION["carrinho"]);
Esta gerando o objeto CarrinhoDao repetidas vezes e o carrinho sempre fica com 1 produto apenas.
$_SESSION["carrinho"] = serialize(new CarrinhoDao());
}
$CarrinhoDao = unserialize($_SESSION["carrinho"]);