Created
January 13, 2014 23:13
-
-
Save davidchc/8409951 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//atribur a session a variavel carrinho | |
$cart = $_SESSION['carrinho']; | |
//seleciona apenas o IDS do carrinho | |
$ids = array_keys($cart); | |
//Seleciona os produtos com os ids | |
$query = mysql_query("SELECT * FROM produtos WHERE id IN(".implode(',', $ids).")"); | |
//Inicia a variavel com 0 | |
$total = 0; | |
?> | |
<table> | |
<caption>Carrinho de Compras</caption> | |
<thead> | |
<tr> | |
<th width="244">Produto</th> | |
<th width="89">Preço</th> | |
</tr> | |
</thead> | |
<tbody> | |
<!-- Percorre os registros selecionados pelo ID dos produtos --> | |
<?php while($row = mysql_fetch_assoc($query)) : ?> | |
<?php | |
$id = $row['id']; //id do produto | |
$qtd = $cart[$id]; //quantidade do produto | |
$produto = $row['produto']; //nome do produto | |
$preco = $row['preco']; //preco do produto | |
$total += $preco*$qtd; //valor total dos produtos | |
?> | |
<!-- | |
Aqui fazemos para exibir os produtos, de acordo com a quantidade | |
adicionada. Se for 2 quantidade de 1 produto, mostrar 2 vezes o produto | |
--> | |
<?php for($i = 1; $i<= $qtd; $i++) : ?> | |
<tr> | |
<td><?php echo $produto; ?></td> | |
<td>R$ <?php echo number_format($preco, 2, ',', '.'); ?></td> | |
</tr> | |
<?php endfor;?> | |
<?php endwhile;?> | |
<!-- Exibi o valor total--> | |
<tr> | |
<td colspan="2">Total: R$ <?php echo number_format($total, 2, ',', '.'); ?></td> | |
</tr> | |
</tbody> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment