Sue

loja

Sue
Jun 15th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. <?php
  2. session_start();
  3. if(!isset($_SESSION['carrinho'])){
  4. $_SESSION['carrinho'] = array();
  5. }
  6.  
  7. // Adiciona produto
  8.  
  9. if(isset($_GET['acao'])){
  10.  
  11. //Adicionar carrinho
  12. if($_GET['acao'] == 'add'){
  13. $id = intval ($_GET['id']);
  14. if(!isset($_SESSION['carrinho'][$id])){
  15. $_SESSION['carrinho'][$id] = 1;
  16. }
  17. }
  18.  
  19. //Remover carrinho
  20. if($_GET['acao'] == 'del'){
  21. $id = intval ($_GET['id']);
  22. if(isset($_SESSION['carrinho'][$id])){
  23. unset($_SESSION['carrinho'][$id]);
  24. }
  25. }
  26.  
  27. //alterar quantidade
  28. if($_GET['acao'] == 'up'){
  29. if (is_array($_POST['prod'])){
  30. foreach($_POST['prod'] as $id => $qtd){
  31. $id = intval ($id);
  32. $qtd = intval ($qtd);
  33. if (!empty($qtd) || $qtd <> 0){
  34. $_SESSION['carrinho'][$id] = $qtd;
  35. }else{
  36. unset($_SESSION['carrinho'][$id]);
  37. }
  38. }
  39. }
  40. }
  41. }
  42. ?>
  43.  
  44. <?php
  45. include ("verifica.php");
  46. ?>
  47. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  48. <html xmlns="http://www.w3.org/1999/xhtml">
  49. <head>
  50.  
  51. <?php
  52. include "../admin/ligacao.php";
  53. ?>
  54.  
  55. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  56. <title>loja</title>
  57.  
  58. <style type="text/css">
  59. <!--
  60. @import url(../css.css);
  61. -->
  62. </style>
  63.  
  64. </head>
  65.  
  66. <body>
  67.  
  68.  
  69. <div id="conteudo">
  70.  
  71. <b class="titulo">Carrinho de Compras</b>
  72.  
  73. <div id="texto">
  74.  
  75. <table border="0px" style="text-align:center">
  76.  
  77. <tr><td colspan="6" style="text-align:right"><a href="categoria1.php" class="formulario" style="text-decoration:none;"> Continuar as compras </a></td>
  78. </tr>
  79.  
  80. <tr height="5px"></tr>
  81.  
  82. <tr>
  83. </tr>
  84.  
  85. <tr>
  86. <td width="200"><b>Nome do Produto</b></td>
  87. <td width="80"><b>Imagem</b></td>
  88. <td width="79"><b>Quantidade</b></td>
  89. <td width="75"><b>Preço</b></td>
  90. <td width="90"><b>Subtotal</b></td>
  91. <td width="64"><b>Acções</b></td>
  92. </tr>
  93.  
  94. <form action="?acao=up" method="post">
  95.  
  96. <?php
  97. if (count($_SESSION['carrinho'])== 0){
  98. echo '<tr> <td colspan="5"> Não há produtos no carrinho </td></tr>';
  99. }else{
  100.  
  101. foreach ($_SESSION['carrinho'] as $id => $qtd){
  102. $sql = "SELECT * FROM produtos WHERE id= '$id'";
  103. $sql = mysql_query($sql) or die (mysql_error());
  104. $linha = mysql_fetch_assoc($sql);
  105.  
  106. $nome_produto = $linha['nome_produto'];
  107. $imagem = $linha['imagem'];
  108. $preco = number_format ($linha ['preco'], 2, ',', '');
  109. $sub = number_format ($linha ['preco'] * $qtd, 2, ',', '');
  110.  
  111. $total += $sub;
  112.  
  113. echo '<tr>
  114. <td> '.$nome_produto.' </td>
  115. <td><img src="../imagens/'.$imagem.'" style="max-width:60px; max-height:60px;" border="0px" /> </td>
  116. <td> <input type="text" size="3" name="prod['.$id.']" value="'.$qtd.'" style="text-align:center"/> </td>
  117. <td> '.$preco.' €</td>
  118. <td> '.$sub.' € </td>
  119. <td> <a href="?acao=del&id='.$id.'"><span class="formulario"> Apagar</span> </a> </td>
  120. </tr>';
  121. }
  122. $total = number_format ($total, 2, ',', '');
  123. echo '<tr></tr><tr>
  124. <td colspan ="3"></td>
  125. <td><b>Total</b></td>
  126. <td> '.$total.' € </td>
  127. </tr><tr height="5px"></tr>';
  128. }
  129.  
  130. ?>
  131.  
  132. <tr>
  133. <td colspan="4"></td>
  134. <td colspan="2"><input type="submit" value="Actualizar Encomenda" class="formulario" style="text-align:right" /></td>
  135. </tr>
  136.  
  137. <tr height="5px">
  138. </tr>
  139.  
  140. <tr>
  141. <td colspan="4"></td>
  142. <td colspan="2"> <a href="finalizar_encomenda.php"><span class="formulario">Finalizar a encomenda</span></a></td>
  143. </tr>
  144.  
  145. <tr height="15px">
  146. </tr>
  147.  
  148. </form>
  149. </table>
  150. </div><!--fim div texto-->
  151. </div> <!--fim div conteudo-->
  152.  
  153.  
  154.  
  155. </body>
  156. </html>
Advertisement
Add Comment
Please, Sign In to add comment