funcao Foreach
Bom, estou com um problema no meu código aqui, não liguem pelo código esta feio, esta documentando, e está funcionando, bom o meu problema é com a funcao FOREACH, não sei usar muito bem, to com um site aonde eu adiciono os produtos ao carrinho, e ali ponho a quantidade que quero, até ai funciona, mas alem de quantidade eu queria botar outro campo metros, pois é de madeiras, ali o cliente botaria o tamanho da madeira, ex: 3.5, ai ele atualizaria o valor da unidade e do total, enfim ja tentei de todos os jeitos e nenhum deu certo, até pq não sei fazer o devido uso do FOREACH, codigo a baixo..
<?php
session_start();
if(!isset($_SESSION['carrinho'])){
$_SESSION['carrinho'] = array();
} //adiciona produto
if(isset($_GET['acao'])){
//ADICIONAR CARRINHO
if($_GET['acao'] == 'add'){
$id = intval($_GET['id']);
if(!isset($_SESSION['carrinho'][$id])){
$_SESSION['carrinho'][$id] = 1;
} else {
$_SESSION['carrinho'][$id] += 1;
}
} //REMOVER CARRINHO
if($_GET['acao'] == 'del'){
$id = intval($_GET['id']);
if(isset($_SESSION['carrinho'][$id])){
unset($_SESSION['carrinho'][$id]);
}
} //ALTERAR QUANTIDADE AQUI O PROBLEMA, aonde eu tento botar os METROS
if($_GET['acao'] == 'up'){
if(is_array($_POST['prod'])){
foreach($_POST['prod'] as $id => $qtd){
foreach($_POST['prod'] as $id => $mt){
$id = intval($id);
$qtd = intval($qtd);
$mt = intval($mt);
if(!empty($qtd && $mt) || $qtd && $mt <> 0){
$_SESSION['carrinho'][$id] = $qtd;
$_SESSION['carrinho'][$id] = $mt;
}else{
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>s</title>
</head>
<body>
<table>
<caption>Carrinho de Compras</caption>
<thead>
<tr>
<th width="244">Produto</th>
<th width="79">Quantidade</th>
<th width="79">Metros</th>
<th width="89">Preco</th>
<th width="100">SubTotal</th>
<th width="64">Remover</th>
</tr>
</thead>
<form action="?acao=up" method="post">
<tfoot>
<tr>
<td colspan="5"><input type="submit" value="Atualizar Carrinho" /></td>
<tr>
<td colspan="5"><a href="teste.php">Continuar Comprando</a></td>
</tfoot>
<tbody>
<?php
if(count($_SESSION['carrinho']) == 0){
echo '
<tr>
<td colspan="5">Não há produto no carrinho</td>
</tr>
';
} else {
require("conexao.php");
$total = 0;
foreach($_SESSION['carrinho'] as $id => $qtd){
foreach($_SESSION['carrinho'] as $id => $mt){
$sql = "SELECT
p.id AS idProd,
p.nome AS produto,
p.largura,
p.comprimento,
p.estilo,
p.valor,
p.valorCompra,
p.estoque,
p.imagem,
c.id AS idCat,
c.nome AS categoria,
p.vendidos,
p.tipo,
p.detalhe
FROM
produtos p
INNER JOIN
categorias c ON p.categorias_id=c.id
ORDER BY p.nome ASC";
$res = mysqli_query($con, $sql);
$linha = mysqli_fetch_array($res);
$nome = $linha['produto'];
$preco = number_format($linha['valor'], 2, ',', '.');
$sub = number_format($linha['valor'] * $qtd, 2, ',', '.');
$total += $linha['valor'] * $qtd;
echo '
<tr>
<td>'.$nome.'</td>
<td><input type="text" size="3" name="prod['.$id.']" value="'.$qtd.'" /></td>
<td><input type="text" size="3" name="prod['.$id.']" value="'.$mt.'" /></td>
<td>R$ '.$preco.'</td>
<td>R$ '.$sub.'</td>
<td><a href="?acao=del&id='.$id.'">Remove</a></td>
</tr>';
}
$total = number_format($total, 2, ',', '.');
echo '<tr>
<td colspan="4">Total</td>
<td>R$ '.$total.'</td>
</tr>';
}
}
?>
</tbody>
</form>
</table>
</body>
</html>
.Discussão (2)
Carregando comentários...