Last active
January 3, 2016 04:39
-
-
Save davidchc/8410347 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 | |
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 | |
if($_GET['acao'] == 'up'){ | |
if(is_array($_POST['prod'])){ | |
foreach($_POST['prod'] as $id => $qtd){ | |
$id = intval($id); | |
$qtd = intval($qtd); | |
if(!empty($qtd) || $qtd <> 0){ | |
$_SESSION['carrinho'][$id] = $qtd; | |
}else{ | |
unset($_SESSION['carrinho'][$id]); | |
} | |
} | |
} | |
} | |
} | |
?> | |
<table width="90%" > | |
<div class="container"> | |
<div class="row"> | |
<div class="col-sm-12 col-md-10 col-md-offset-1"> | |
<form action="?pg=carrinho&acao=up" method="post"> | |
<table class="table table-hover"> | |
<thead> | |
<tr> | |
<th>Produto</th> | |
<th>Quantidade</th> | |
<th class="text-center">Valor</th> | |
<th class="text-center">Total</th> | |
<th></th> | |
</tr> | |
</thead> | |
<tbody> | |
<tr> | |
<?php if(count($_SESSION['carrinho']) == 0){ ?> | |
<tr> | |
<td colspan="5"><center>Não há produto no carrinho</center></td></tr><br/> | |
<tr> | |
<td></td> | |
<td></td> | |
<td></td> | |
<td></td> | |
<td> | |
<a href ="?pg=produtos"><input type="button" class="btn btn-default" value="Ir as Compras" /></a> | |
</td> | |
</tr> | |
<?php }else{ | |
$total = 0; | |
foreach($_SESSION['carrinho'] as $id => $qtd){ | |
$sql = "SELECT * FROM produtos WHERE id= '$id'"; | |
$qr = mysql_query($sql) or die(mysql_error()); | |
$ln = mysql_fetch_assoc($qr); | |
$nome1 = $ln['nome']; | |
$descricao = $ln['descricao']; | |
$preco = number_format($ln['preco'], 2, ',', '.'); | |
$sub = number_format($ln['preco'] * $qtd, 2, ',', '.'); | |
$total += $ln['preco'] * $qtd; | |
?> | |
<?php for($i = 1; $i <= $qtd; $i++) {?> | |
<tr> | |
<td class="col-sm-8 col-md-6"> | |
<div class="media"> | |
<a class="thumbnail pull-left" href="#"> <img class="media-object" src="http://icons.iconarchive.com/icons/custom-icon-design/flatastic-2/72/product-icon.png" style="width: 72px; height: 72px;"> </a> | |
<div class="media-body"> | |
<h4 class="media-heading"><a href="#">'.$nome1.'</a></h4> | |
<h5 class="media-heading"> Descrição : <a href="#">'.$descricao.'</a></h5> | |
</div> | |
</div> | |
</td> | |
<td class="col-sm-1 col-md-1" style="text-align: left;"><?php echo $i;?> </td> | |
<td class="col-sm-1 col-md-1 text-center"><strong>R$ <?php echo $preco; ?></strong></td> | |
<td class="col-sm-1 col-md-1 text-center"><strong>R$ <?php echo $sub; ?></strong></td> | |
<td class="col-sm-1 col-md-1"> | |
<a href ="?pg=carrinho&acao=del&id=<?php echo $id; ?>"><input type="button" class="btn btn-danger" value="Remover" /></a> | |
</td> | |
</tr> | |
<?php } //fecha o FOR ?> | |
<?php } //fechao While?> | |
<tr> | |
<td></td> | |
<td></td> | |
<td></td> | |
<td style="text-align: right;"><h4>Total:</h4></td> | |
<td class="text-right"><h4><strong> R$ <?php number_format($total, 2, ',', '.')?> </strong></h4></td> | |
</tr> | |
<tr> | |
<td></td> | |
<td></td> | |
<td></td> | |
<td> | |
<a href ="?pg=produtos"><input type="button" class="btn btn-default" value="Continuar comprando" /></a> | |
<!-- | |
<button type="button" class="btn btn-default"> | |
<span class="glyphicon glyphicon-shopping-cart"></span> Continuar comprando | |
</button>--> | |
</td> | |
<td> | |
<button type="button" class="btn btn-success"> | |
Finalizar Pedido <span class="glyphicon glyphicon-play"></span> | |
</button></td> | |
</tr> | |
<td colspan="5"><input type="submit" class="btn btn-default" value="Atualizar Carrinho" /></td> | |
<?php } ?> | |
<!--<tr> | |
<td></td> | |
<td></td> | |
<td></td> | |
<td><h5>Subtotal</h5></td> | |
<td class="text-right"><h5><strong>$24.59</strong></h5></td> | |
</tr>--> | |
</tbody> | |
</table> | |
</form> | |
</div> | |
</div> | |
</div> | |
</table> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment