Ver Mensaje Individual
  #5 (permalink)  
Antiguo 15/07/2012, 20:29
Avatar de Nemutagk
Nemutagk
Colaborador
 
Fecha de Ingreso: marzo-2004
Ubicación: México
Mensajes: 2.633
Antigüedad: 20 años
Puntos: 406
Respuesta: Sumar productos en carro de compras

Te dejaré un ejemplo...

Código PHP:
Ver original
  1. <?php
  2.  
  3. function en_carrito($search) {
  4.     foreach($_SESSION['carrito'] as $item) {
  5.         if ($search == $item['item']) {
  6.             return true;
  7.             break;
  8.         }
  9.     }
  10.  
  11.     return false;
  12. }
  13. $items = $_SESSION['carrito'];
  14. if (isset($_POST['item'])) {
  15.     $item = $_POST['item'];
  16.    
  17.     if (en_carrito($item)) {
  18.         $items[$item]['propiedades']['cantidad'] = $items[$item]['propiedades']['cantidad'] * $_POST['cantidad'];
  19.     }else {
  20.         $items[$item] = array(
  21.                 'item'=>$item,
  22.                 'propiedades'=>array(
  23.                         'cantidad'=>$_POST['cantidad'],
  24.                         'precio'=>$_POST['precio']
  25.                     )
  26.             );
  27.     }
  28.  
  29.     $_SESSION['carrito'] = $items;
  30. }
  31. ?>
  32. <html>
  33.     <head>
  34.         <title>Carrito</title>
  35.     </head>
  36.     <body>
  37.         <form action="carrito.php" method="post">
  38.             <input type="text" name="item" id="">
  39.             <input type="text" name="cantidad" id="">
  40.             <input type="text" name="precio" id="">
  41.             <input type="submit" value="Enviar">
  42.         </form>
  43.         <div>
  44.             <?php
  45.             if (count($items) != 0) {
  46.                 foreach($items as $item => $valor) {
  47.                     echo '<p>item: '.$valor['item'].' - '.$valor['propiedades']['cantidad'].' - '.$valor['propiedades']['precio'].' - '.($valor['propiedades']['precio'] * $valor['propiedades']['cantidad']).'</p>';
  48.                 }
  49.             }else {
  50.                 echo '<p>No hay items en el carrito</p>';
  51.             }
  52.         </div>
  53.     </body>
  54. </html>
__________________
Listo?, tendría que tener 60 puntos menos de IQ para considerarme listo!!!
-- Sheldon Cooper
http://twitter.com/nemutagk
PD: No contestaré temas vía mensaje personal =)