Ver Mensaje Individual
  #1 (permalink)  
Antiguo 29/08/2011, 09:12
leo_star
 
Fecha de Ingreso: agosto-2011
Mensajes: 20
Antigüedad: 12 años, 8 meses
Puntos: 0
Pregunta Problemas con mi carrito de compras

Hola, estoy haciendo un carrito de compras y bueno tengo un problemita; al agregar un producto siempre me sale el ultimo producto en la lista:

dejo el código de la función que procesa el carrito:

Código PHP:
Ver original
  1. function imprime_carrito(){
  2.         $suma = 0;
  3.         echo '<table border=1 cellpadding="3">
  4.               <tr>
  5.                 <td><b>Nombre producto</b></td>
  6.                 <td><b>Precio</b></td>
  7.                                <td><b>Cant.</b></td>
  8.                                <td><b>SubTotal</b></td>
  9.                 <td>&nbsp;</td>
  10.               </tr>';
  11.         for ($i=0;$i<$this->num_productos;$i++){
  12.             if($this->array_id_prod[$i]!=0){
  13.                 echo '<tr>';
  14.                 echo "<td>" . $this->array_nombre_prod[$i] . "</td>";
  15.                 echo "<td>" . $this->array_precio_prod[$i] . "</td>";
  16.                                 echo "<td>" . $this->array_cantidad_prod[$i] . "</td>";
  17.                                 echo "<td>" . $this->array_cantidad_prod[$i] * $this->array_precio_prod[$i] . "</td>";
  18.                                
  19.                 echo "<td><a href='eliminar_producto.php?linea=$i'><img src='images/carro_eliminar.gif'/></td>";
  20.                 echo '</tr>';
  21.                 $suma += $this->array_precio_prod[$i];
  22.             }
  23.         }
  24.         //muestro el total
  25.         echo "<tr><td><b>TOTAL:</b></td><td> <b>$suma</b></td><td>&nbsp;</td></tr>";
  26.         //total más IVA
  27.         echo "<tr><td><b>IVA (16%):</b></td><td> <b>" . $suma * 1.16 . "</b></td><td>&nbsp;</td></tr>";
  28.         echo "</table>";
  29.     }


ahora el código donde muestro los productos en el sitio:

Código PHP:
Ver original
  1. <?php
  2.           $x= mysql_num_rows($query);
  3.             $i= 0;
  4.                while($row= mysql_fetch_assoc($query))
  5.              {                                
  6.                
  7.              ?>
  8.            <div class="producto" id="<?php $row['nom_prod']; ?>">
  9.              <img src="productos/<?php echo $row['id_prod']?>.jpg" height="130" width="130"  align="left"/>
  10.             <?php
  11.                 echo "<h1>" .$row['nom_prod'], "</h1>";
  12.                 $nombre= $row['nom_prod'];
  13.              ?>
  14.                 <p><?php echo $row['coment_prod']; ?></p>
  15.                
  16.              <form action="agregar_producto.php" method="post">
  17.                 Cantidad: <input name="cantidad" type="text" id="cantidad" size="2" />
  18.                     <?php
  19.                             echo "# ID: " .$row['id_prod'], ", ";
  20.                             $id = $row['id_prod'];
  21.                             echo "Precio: " .$row['precio_prod'], " ";
  22.                             $precio= $row['precio_prod'];
  23.                            
  24.                             $ids[]=$id;
  25.                             $nombres[]=$nombre;
  26.                             $precios[]=$precio;
  27.                      
  28.                    
  29.                             $_SESSION["nom_prod"]= $nombres[$i];
  30.                             $_SESSION["id_prod"]= $ids[$i];
  31.                             $_SESSION["precio_prod"]= $precios[$i];  
  32.                            $i= $i+1;
  33.                            
  34.                     ?>  
  35.                 <input type="submit"  value="A&ntilde;adir al Carro" />
  36.                
  37.  
  38.              </form>  
  39.                
  40.                      
  41.            </div>
  42.            <?php                
  43.              }
  44.            ?>
  45.  
  46. Entiendo que el problema podría estar en el WHILE, por eso le puse un i++
  47. pero aun así no se me soluciona...
  48.  
  49. ojala me puedan ayudar, desde ya muchas gracias!!