Foros del Web » Programando para Internet » PHP »

listado de productos

Estas en el tema de listado de productos en el foro de PHP en Foros del Web. Hola a todos y muchas gracias de ante mano espero que me puedan ayudar tengo un inconveniente con un carro de compras. Que consta de ...
  #1 (permalink)  
Antiguo 20/08/2011, 23:18
 
Fecha de Ingreso: noviembre-2008
Mensajes: 28
Antigüedad: 15 años, 5 meses
Puntos: 0
listado de productos

Hola a todos y muchas gracias de ante mano espero que me puedan ayudar tengo un inconveniente con un carro de compras. Que consta de un listado de categorias donde se selecciona la categoria y despliega los productos de dicha categoria hasta ahi todo funciona correctamente. Mi problema comienza cuando los voy agregar al carrito, no envia ningun producto a la pagina ver_carro, como podira hacer para agregar los productos al carro?. A continuacion posteo mi codigo y espero que me puedan ayudar y muchas gracias a todos nuevamente.

listado_categoria.php
===============================

<?php
include("conector.php");
$sql = "select * from categoria";
$result = mysql_query($sql, $conexion) or die (mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LESICOS</title>
</head>

<body>
<table border="1" align="center" width="200" cellpadding="2" cellspacing="2">
<tr align="center">
<td>Categoria</td>
</tr>
<?php if(mysql_num_rows($result) > 0)
{
while($fila = mysql_fetch_assoc($result))
{
?>
<tr align="center">
<td><a href="?pagina=listado_producto&id_categoria=<?php echo $fila["id_categoria"];?>"><?php echo $fila["categoria"];?></a></td>
</tr>
<?php
}//fin del while
}//fin del if
else{
echo "No se encontraron registros para mostrar";
}//fin del else
?>
</table>
<?php mysql_free_result($result);?>
</body>
</html>


Listado_productos.php
=================================

<?php
include("conector.php");
$sql = "select * from producto where id_categoria = '".$_GET['id_categoria']."'";
$result = mysql_query($sql, $conexion) or die (mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LESICOS</title>
</head>

<body>
<form method="post" action="agregar.php">
<table border="1" align="center" width="300" cellpadding="2" cellspacing="2">
<tr align="center">
<td>Producto</td>
<td>Precio</td>
<td>Cantidad</td>
<td><a href="agregar.php?id_prod=<?php $fila["id_prod"];?>"><img src="img/agregar.png" height="32" width="32" border="0"></a></td>
</tr>
<?php if(mysql_num_rows($result) > 0)
{
while($fila = mysql_fetch_assoc($result))
{
?>
<tr align="center">
<td><?php echo $fila["nom_prod"];?></td>
<td><?php echo $fila["precio"];?></td>
<td>
<select name="cantidad[<?php echo $fila["id_prod"];?>]">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
<td><input type="checkbox" name="seleccion" value="<?php echo $fila["id_prod"];?>"></td>
</tr>
<?php
}//fin del while
}//fin del if
else{
echo "No se encontraron datos para mostrar";
}//fin del else
?>
</table>
</form>
<?php
mysql_free_result($result);
?>
</body>
</html>

agregar.php
===========================

<?php
session_start();
$id_prod=$_POST['id_prod'];
$cantidad=$_POST['cantidad'];
$seleccion=$_POST['seleccion'];
$carro[$id_prod] = array(
"codigo" => $_POST['id_prod'],
"nom_prod" => $_POST['nom_prod'],
"cantidad" => $_POST['cantidad'],
"precio" => $_POST['precio'],
"seleccion" => $_POST['seleccion'],
);
$_SESSION['carro']=$carro;
header("Location:agregar_carrito.php");
?>
  #2 (permalink)  
Antiguo 20/08/2011, 23:49
Avatar de andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 8 meses
Puntos: 793
Respuesta: listado de productos

Hola @kushanku, debes decidir por que método vas a enviar los datos si por $_GET o $_POST...

agregar.php?id_prod=<?php $fila["id_prod"];?>

"envias" id_prod por $_GET y lo intentas recibir por $_POST

id_prod=$_POST['id_prod'];

<?php $fila["id_prod"];?> aquí nisiquiera imprimes la variable

cantidad[<?php echo $fila["id_prod"];?>] La cantidad lo tienes como un array y por último no veo como envias el formulario.

Lee esto: Variables Desde Fuentes Externas
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP

Última edición por andresdzphp; 21/08/2011 a las 00:06
  #3 (permalink)  
Antiguo 21/08/2011, 00:15
 
Fecha de Ingreso: noviembre-2008
Mensajes: 28
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: listado de productos

Hola andreszphp muchas gracias por responder creo ya corregi lo que mencionas pero ahora el error envia en el archivo agregar_carrito.php en la linea 29, no se que puede estar mal este es el error. Me podrias ayudar nuevamente por favor.

Fatal error: Unsupported operand types in C:\AppServ\www\aplicacion producto\carrito_de_compras\agregar_carrito.php on line 29

agregar_carrito.php
===========================

<?php
session_start();
if (!count($_SESSION['carro'])){
session_destroy();
unset($_SESSION['carro']);
echo "No hay compras en el carro...";
exit;
}
?>
<html>
<head>
<title>PRODUCTOS AGREGADOS AL CARRITO</title>
</head>
<body>
<p align="center">CARRITO DE COMPRAS </p>
<table width="609" border="1" cellspacing="0" cellpadding="0" align="center">
<tr class="prod">
<td width="45"><span class="Estilo7">Nro</span></td>
<td width="329"><span class="Estilo7">Producto</span></td>
<td width="41"><span class="Estilo7">Precio</span></td>
<td width="67" align="center"><span class="Estilo7">Cantidad</span></td>
<td width="59" align="center"><span class="Estilo7">Importe</span></td>
<td width="54" align="center"><span class="Estilo7">Borrar</span></td>
</tr>
<?php
$contador=0;
$st=0;
foreach($carro as $k => $v){
$importe=$v['cantidad']*$v['precio'];
$st=$st+$importe;
$contador++;
?>
<tr>
<td><div align="center"><?php echo $contador; ?></div></td>
<td><?php echo $v['nom_prod'] ?></td>
<td><?php echo $v['precio'] ?></td>
<td><?php echo $v['cantidad'] ?> </td>
<td><strong><?php echo $importe; ?></strong> </td>
<td width="54" align="center"> <a href="borrar.php?id=<?php echo $v['codigo'] ?>">quitar</a></td>
</tr>
<?php } ?>
</table>
<table width="614" border="0" cellspacing="0" cellpadding="0" align="center">
<tr class="prod">
<td width="53">&nbsp;</td>
<td width="340">&nbsp;</td>
<td width="55">&nbsp;</td>
<td width="85" align="center"><div align="left">Sub Total </div></td>
<td width="43" align="center"><div align="left"><strong><?php echo $st; ?></strong></div></td>
<td width="38" align="center">&nbsp;</td>
</tr>
<tr class="prod">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="center"><div align="left">IGV</div></td>
<td align="center"><div align="left"><strong><?php echo $st*0.19; ?></strong></div></td>
<td align="center">&nbsp;</td>
</tr>
<tr class="prod">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="center"><div align="left">Total Pago </div></td>
<td align="center"><div align="left"><strong><?php echo $st+($st*0.19); ?></strong></div></td>
<td align="center">&nbsp;</td>
</tr>
<tr class="prod">
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td align="center"><div align="left"></div></td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr class="prod">
<td colspan="6">[<a href="javascript:;">Continuar comprando</a>] [<a href="javascript:;">Vaciar carrito de compras</a>] [<a href="javascript:;"><strong>Cerrar Session</strong></a>] [<a href="ordenar_compra.php">Finalizar Compra</a>]
<div align="left"></div></td>
</tr>
<?php
$contador=0;
$suma=0;
foreach($carro as $k => $v){
$importe=$v['cantidad']*$v['precio'];
$suma=$suma+$subto;
$contador++;
?>
<?php
}
?>
</table>
<p>&nbsp;</p>
</body>
</html>
  #4 (permalink)  
Antiguo 21/08/2011, 21:50
 
Fecha de Ingreso: noviembre-2008
Mensajes: 28
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: listado de productos

hola a todos y muchas gracias de ante mano. Me podrian ayudar por favor estoy haciendo un carro de compras me tranque en la parte de agregar productos al carro. No se si lo estoy haciendo correctamente me podrian guiar por favor. Tengo un listado de productos con un <select name="cantidad[]"> para elegir la cantidad y un checkbox nane="seleccion[]" para seleccionar los productos. Hice lo siguiente pero no me funciona agregar.php necesito ayuda por favor.

este es el archivo del listado.php
==========================

<?php
include("conector.php");
$sql = "select * from producto where id_categoria = '".$_GET['id_categoria']."'";
$result = mysql_query($sql, $conexion) or die (mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LESICOS</title>
</head>

<body>
<form method="post" action="agregar.php">
<table border="1" align="center" width="300" cellpadding="2" cellspacing="2">
<tr align="center">
<td>Producto</td>
<td>Precio</td>
<td>Cantidad</td>
<td><input type="submit" name="enviar" value="Agregar"></td>
</tr>
<?php
if(mysql_num_rows($result) > 0)
{
//$contador = 0;
while($fila = mysql_fetch_assoc($result))
{
?>
<tr align="center">
<td><?php echo $fila["nom_prod"];?></td>
<td><?php echo $fila["precio"];?></td>
<td>
<select name="cantidad[<?php echo $contador; ?>]">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</td>
<td><?php echo"<input type='checkbox' name='seleccion[$contador]' value='$fila[nom_prod]'/>";?></td>
</tr>
<?php
}//fin del while
}//fin del if
else{
echo "No se encontraron datos para mostrar";
}//fin del else
?>
</table>
</form>
<?php
mysql_free_result($result);
?>
</body>
</html>

y este el archivo agregar.php
=========================

<?php
session_start();
$id_prod =$_POST['id_prod'];
$cantidad=$_POST['cantidad'];
$seleccion=$_POST['seleccion'];
$precio=$_POST['precio'];
if(count($seleccion) > 0){
foreach($seleccion as $contador => $valor)
{
$subtotal = $valor[cantidad] * $valor[precio];
$suma = $suma + $subtotal;
$productos .= "$valor cantidad $cantidad[$contador] precio $suma ";
}
}
echo $productos;
$carro[$productos] = array(
"id_prod" => $_POST['id_prod'],
"nom_prod" => $_POST['nom_prod'],
"cantidad" => $_POST['cantidad'],
"precio" => $_POST['precio'],
"seleccion" => $_POST['seleccion'],
);
$_SESSION['carro']=$carro;
header("Location:agregar_carrito.php");
?>
  #5 (permalink)  
Antiguo 26/09/2011, 14:16
 
Fecha de Ingreso: noviembre-2008
Mensajes: 28
Antigüedad: 15 años, 5 meses
Puntos: 0
Respuesta: listado de productos

hola a todos por favor me podrian ayudar a agregar los productos al carro mi codigo ya lo habia puesto. Muchas gracias a todos.

Etiquetas: html, listado, mysql, productos, registro, sql
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:34.