Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal,
Vê se alguém consegue observar essa notificação, pois já olhei e nada até agora?
Veja código mov_entrada.php
<?php
if(isset($_POST['incluir']))
{
$produtos = isset($_POST['produto']) ? $_POST['produto'] : '';
$valores = array($produtos);
if(!isset($_SESSION['carrinho'])){
$_SESSION['carrinho'] = array();
}
//adiciona produto
if(isset($_GET['acao'])){
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$id = $valores[0][0];
$id = intval($valores[0][0]);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][] = $valores;
}
}
}
}
//REMOVER CARRINHO
if(isset($_GET['acao'])){
if($_GET['acao'] == 'del'){
$id = intval($_GET['id']);
if(isset($_SESSION['carrinho'][$id])){
unset($_SESSION['carrinho'][$id]);
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Itens dos Produtos</title>
</head>
<body>
<table>
<thead>
<tr>
<th width="80">Codigo</th>
<th width="400">Produto</th>
<th width="79">Quantidade</th>
<th width="89">Preço</th>
<th width="100">SubTotal</th>
<th width="64">Remover</th>
</tr>
</thead>
<form action="?cad=entradas_p1" method="post">
<tfoot>
<tr>
<td colspan="5"><input type="submit" value="Gravar" /></td>
</tr>
</tfoot>
<tbody>
<?php
if(count($_SESSION['carrinho']) == 0){
echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';
}else{
require("conexao/bdinc.php");
$total = 0;
foreach ($_SESSION['carrinho'] as $id => $valores) {
// $arr[3] será atualizado com cada valor de $arr...
$cod = $valores[0][0];
$nome = $valores[0][1];
$preco = str_replace(",",".",$valores[0][2]);
$qtd = $valores[0][3];
$sub = number_format($preco * $qtd, 2, ',', '.');
$total += $preco * $qtd;
$_SESSION['total'] = $total;
echo( '<tr>
<td>'.$cod.'</td>
<td>'.$nome.'</td>
<td align="center">'.$qtd.'</td>
<td align="right">R$'.$preco.'</td>
<td align="right">R$ '.$sub.'</td>
<td><a href="?cad=entradas&acao=del&id='.$id.'">Remove</a></td>
</tr>');
}
$total = number_format($total, 2, ',', '.');
echo '<tr>
<td colspan="4">Total</td>
<td align="right">R$ '.$total.'</td>
</tr>';
}
?>
</tbody>
</form>
</table>
</body>
</html>
A mensagem de notificação acusa uma variável indefinida nessa linha 69;
if(count($_SESSION['carrinho']) == 0){Williams Duarte,
Quando coloquei sua sugestão agora exibe isso:
Notice: Undefined index: carrinho in C:\Apache24\htdocs\estoque\mov_entradas.php on line 77
Warning: Invalid argument supplied for foreach() in C:\Apache24\htdocs\estoque\mov_entradas.php on line 77
A mensagem de notificação vem dessa linha.
foreach ($_SESSION['carrinho'] as $id => $valores) {
if (isset($_SESSION['carrinho']) && is_array($_SESSION['carrinho'])) {
foreach ($_SESSION['carrinho'] as $id => $valores) {
....
}
}
Resumindo, se a variável precisa ser setada em algum bloco antes, então sempre verifique se esta já foi iniciada antes, para isso utilize ISSET sempre, assim o bloco dentro do ISSET só sera executada, se os valores destas já existir ou seja retornar TRUE.Williams Duarte,
Muito obrigado agora deu certinho.
Pode me ajudar como fazer nessa parte do código não aceita valores que já existe no array da session:
//adiciona produto
if(isset($_GET['acao'])){
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$id = $valores[0][0];
$id = intval($valores[0][0]);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][] = $valores;
}
}
}
}Só verificar se já existe um indice com ID do Produto
if($_GET['acao'] == 'add'){
//Verifica se o Array já existe, caso não, cria
if (!isset($_SESSION['carrinho']) {
$_SESSION['carrinho'] = array();
}
//Senão existir cria outro indice
$id = intval($valores[0][0]);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][$id] = 1;
}
}
Este 1
e a quantidade de produto selecionado
$_SESSION['carrinho'][$id] = 1;
voce pode colocar dinamico
$qtd = intval($qtd);
$_SESSION['carrinho'][$id] = $qtd;
Pra atualizar ou até mesmo recalcular, e se tiver mais de um produto na lista, use foreach para percorrer o indice
Williams Duarte,
Muito obrigado pela ajuda.
Agora veja código para alguém precisar.
//ADICIONA PRODUTO NO CARRINHO
if(isset($_GET['acao'])){
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$id = intval($valores[0][0]);
if (!isset($_SESSION['carrinho'][$id])) {
$_SESSION['carrinho'][$id] = $valores;
}else{
echo "<script language=JavaScript> window.alert('O item $id consta no carrinho !'); </script>";
echo "<script>window.location = 'cadastros.php?cad=entradas'</script>";
}
}
}
};)
Use isset para verificar se ela foi iniciada!
if(isset($_SESSION['carrinho']) && count($_SESSION['carrinho']) <= 0){