
26/06/2009, 04:35
|
| | Fecha de Ingreso: febrero-2005
Mensajes: 12
Antigüedad: 20 años, 1 mes Puntos: 0 | |
problema carro compra Muy buenas, tengo funcionando correctamente un carro de compra, pero me ha surgido un problema: cuando se compra un mismo producto varias veces, al listar los productos introducidos en el carrito, aparece en cada linea un producto con la cantidad comprada de ese producto; pero necesito que en lugar de aumentar la cantidad del producto, se muestre el producto comprado repetido en lineas distintas. Que si del producto id=300 he comprado dos unidades, se muestren dos lineas del mismo producto en la vista del carrito.
Y no logro dar con la solución.
El codigo del carro es:
web de vercarrito.php:
<?php
session_start();
if(isset($_SESSION['carro']))
$carro=$_SESSION['carro'];else $carro=false;
?>
<html>
<head>
<title>PRODUCTOS AGREGADOS AL CARRITO</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1 align="center">Carrito</h1>
<?php
if($carro){
//si el carro no está vacío,
//mostramos los productos
?>
<table width="720" border="0" cellspacing="0" cellpadding="0" align="center">
<tr bgcolor="#333333" class="tit">
<td width="105">Producto</td>
<td width="207">Precio</td>
<td colspan="2" align="center">Cantidad de Unidades</td>
<td width="100" align="center">Borrar</td>
</tr>
<?php
$suma=0;
foreach($carro as $k => $v){
//recorremos la matriz que tiene
//todos los valores del carro,
//calculamos el subtotal y el
// total
$subto=$v['cantidad']*$v['precio'];
$suma=$suma+$subto;
?>
<form name="a<?php echo $v['identificador'] ?>" method="post" action="agregacar.php?<?php echo SID ?>" id="a<?php echo $v['identificador'] ?>">
<tr class='prod'>
<td><?php echo $v['producto'] ?></td>
<td><?php echo $v['precio'] ?></td>
<td width="43" align="center"><?php echo $v['cantidad'] ?></td>
<td width="136" align="center">
<input name="cantidad" type="text" id="cantidad" value="<?php echo $v['cantidad'] ?>" size="8">
<input name="id" type="hidden" id="id" value="<?php echo $v['id'] ?>"> </td>
<td align="center"><a href="borracar.php?<?php echo SID ?>&id=<?php echo $v['id'] ?>"><img src="trash.gif" width="12" height="14" border="0"></a></td>
</tr></form>
<?php
//por cada item creamos un
//formulario que submite a
//agregar producto y un link
//que permite eliminarlos
}
?>
</table>
<div align="center"><span class="prod">Total de Artículos: <?php echo count($carro);
//el total de items va a ser igual
//a la cantidad de elementos que
//tenga la matriz $carro, valor
//que obtenemos con la función
//count o con sizeof
?></span>
</div><br>
<div align="center"><span class="prod">Total: $<?php echo number_format($suma,2);
//mostramos el total de la variable
//$suma formateándola a 2 decimales
?></span>
</div><br>
<div align="center"><span class="prod">Continuar la selección de productos</span>
<a href="catalogo.php?<?php echo SID;?>">
<img src="continuar.gif" width="13" height="13" border="0"></a>
</div>
<?php }else{ ?>
<p align="center"> <span class="prod">No hay productos seleccionados</span>
<a href="catalogo.php?<?php echo SID;?>">
<img src="continuar.gif" width="13" height="13" border="0"></a>
<?php }?>
</p>
</body>
</html>
web de catalogo.php: <?php ob_start("ob_gzhandler"); session_start(); mysql_connect("localhost","usuario","password"); mysql_select_db("db"); if(isset($_SESSION['carro'])) $carro=$_SESSION['carro'];else $carro=false; //y hacemos la consulta $qry=mysql_query("select * from catalogo order by producto asc"); ?> <html> <head> <title>CATÁLOGO</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <table width="272" align="center" cellpadding="0" cellspacing="0" style="border: 1px solid #000000;"> <tr valign="middle" bordercolor="#FFFFFF" bgcolor="#DFDFDF" class="catalogo"> <td width="170"><strong>Producto</strong></td> <td width="77"><strong>Precio</strong></td> <td width="25" align="right"><a href="vercarrito.php?<?php echo SID ?>" title="Ver el contenido del carrito"> <img src="vercarrito.gif" width="25" height="21" border="0"></a></td> </tr> <?php //mostramos todos nuestros //artículos, viendo si han //sido agregados o no a nuestro //carro de compra while($row=mysql_fetch_assoc($qry)){ ?> <tr valign="middle" class="catalogo"> <td><?php echo $row['producto'] ?></td> <td><?php echo $row['precio'] ?></td> <td align="center"> <?php if(!$carro || !isset($carro[md5($row['id'])]['identificador']) || $carro[md5($row['id'])]['identificador']!=md5($row['id'])){ //si el producto no ha sido //agregado, mostramos la imagen //de no agregado, linkeada //a nuestra página de agregar //producto y transmitíéndole a //dicha página el id del artículo //y el identificador de la sesión ?> <a href="agregacar.php?<?php echo SID ?>&id=<?php echo $row['id']; ?>"> <img src="productonoagregado.gif" border="0" title="Agregar al Carrito"></a><?php }else //en caso contrario mostramos la //otra imagen linkeada., a la //página que sirve para borrar el //artículo del carro. {?><a href="borracar.php?<?php echo SID ?>&id=<?php echo $row['id']; ?>"> <img src="productoagregado.gif" border="0" title="Quitar del Carrito"></a><?php } ?></td> </tr><?php } ?> </table> </body> </html> <?php ob_end_flush(); ?> web de agregacar.php: <?php session_start(); extract($_REQUEST); mysql_connect("localhost","usuario","password"); mysql_select_db("db"); if(!isset($cantidad)){$cantidad=1;} $qry=mysql_query("select * from catalogo where id='".$id."'"); $row=mysql_fetch_array($qry); if(isset($_SESSION['carro'])) $carro=$_SESSION['carro']; $carro[md5($id)]=array('identificador'=>md5($id), 'cantidad'=>$cantidad,'producto'=>$row['producto'], 'precio'=>$row['precio'],'id'=>$id); $_SESSION['carro']=$carro; header("Location:catalogo.php?".SID); ?>
Si alguien puede ayudarme se lo agradezco mucho, porque no doy con la solución.
Gracias y saludos. |