estoy queriendo hacer el ejemplo que hay en una pagina web(no se si puedo decirla) de un carrito de compras sencillo pero tengo un problema.
me esta agregando productos (tengo un contador) al carrito pero no me muestra el producto ni el precio . estoy trabado , quizas sea facil el error pero no lo encuentro a ver si alguna alma bondadosa del foro me puede ayudar aqui le dejo el codigo.. supongo que puede ser algun paso de variables.
este archivo es el que muestra los productos >> index php
Código PHP:
<HTML>
<HEAD>
<TITLE></TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body {
margin-top: 0px;
}
-->
</style></HEAD>
<BODY>
<?
require("conexion/conectar.php");
require("conexion/seleccion.php");
//Ejecutamos la sentencia SQL
$result=mysql_query("select * from productos");
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="15%" height="219"> </td>
<td width="85%" align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" bgcolor="#00CCFF"> </td>
</tr>
<tr>
<td width="14%" bgcolor="#CCFFCC"> </td>
<td width="86%"><table width="374" align="center" cellpadding="3" cellspacing="3">
<tr>
<th width="101"><div align="left">producto</div></th>
<th width="115"><div align="left">precio</div></th>
<th width="70"> </th>
</tr>
<?
//Mostramos los registros
while ($row=mysql_fetch_array($result))
{
echo '<tr><td>'.$row["nombre"].'</td>';
echo '<td>'.$row["precio"].'</td>';
echo '<td>'.'<a href="product_info.php?id=id_product&nombre=nombre&precio=precio">'.'ver'.'</a>'.'</td>';
echo '<td>'.'<a href="product_info.php?id=id_product&nombre=nombre&precio=precio">'.'agregar'.'</a>'.'</td></tr>';
}
mysql_free_result($result)
?>
</table></td>
</tr>
</table>
<table width="374" align="center" cellpadding="3" cellspacing="3">
</table>
al darle al boton ver o agregar me mandaria a informacion de producto
product_info.php
Código PHP:
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?
$id = $_POST["id"];
require("conexion/conectar.php");
require("conexion/seleccion.php");
//Ejecutamos la sentencia SQL
$result=mysql_query("select * from productos");
$id = $_POST["id"];
?>
<body>
<table width="66%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">Nombre</td>
<td width="60%">Descripcion</td>
<td width="18%">precio</td>
</tr>
<?
//Mostramos los registros
while ($row=mysql_fetch_array($result))
{
echo '<tr><td>'.$row["nombre"].'</td>';
echo '<td>'.$row["descripcion"].'</td>';
echo '<td>'.$row["precio"].'</td>';
echo '<td>'.'<a href="mete_producto.php?id=id_product&nombre=nombre&precio=precio">'.'agregar'.'</a>'.'</td></tr>';
}
mysql_free_result($result)
?>
</table>
<table width="60%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><a href="ver_carrito.php">ver carrito </a></td>
</tr>
</table>
<p> </p>
</body>
</html>
al dar en agregar mete el producto en el carrito esta es la libreria carrito
lib_carrito.php
Código PHP:
<?
class carrito {
//atributos de la clase
var $num_productos;
var $array_id_prod;
var $array_nombre_prod;
var $array_precio_prod;
//constructor. Realiza las tareas de inicializar los objetos cuando se instancian
//inicializa el numero de productos a 0
function carrito () {
$this->num_productos=0;
}
//Introduce un producto en el carrito. Recibe los datos del producto
//Se encarga de introducir los datos en los arrays del objeto carrito
//luego aumenta en 1 el numero de productos
function introduce_producto($id_prod,$nombre_prod,$precio_prod){
$this->array_id_prod[$this->num_productos]=$id_prod;
$this->array_nombre_prod[$this->num_productos]=$nombre_prod;
$this->array_precio_prod[$this->num_productos]=$precio_prod;
$this->num_productos++;
}
//Muestra el contenido del carrito de la compra
//ademas pone los enlaces para eliminar un producto del carrito
function imprime_carrito(){
$suma = 0;
echo '<table border=1 cellpadding="3">
<tr>
<td><b>Nombre producto</b></td>
<td><b>Precio</b></td>
<td> </td>
</tr>';
for ($i=0;$i<$this->num_productos;$i++){
if($this->array_id_prod[$i]!=0){
echo '<tr>';
echo "<td>" . $this->array_nombre_prod[$i] . "</td>";
echo "<td>" . $this->array_precio_prod[$i] . "</td>";
echo "<td><a href='eliminar_producto.php?linea=$i'>Eliminar producto</td>";
echo '</tr>';
$suma += $this->array_precio_prod[$i];
}
}
//muestro el total
echo "<tr><td><b>TOTAL:</b></td><td> <b>$suma</b></td><td> </td></tr>";
//total más IVA
echo "<tr><td><b>IVA (16%):</b></td><td> <b>" . $suma * 1.16 . "</b></td><td> </td></tr>";
echo "<td>" . "Total de productos=".$this->num_productos . "</td>";
echo "</table>";
}
//elimina un producto del carrito. recibe la linea del carrito que debe eliminar
//no lo elimina realmente, simplemente pone a cero el id, para saber que esta en estado retirado
function elimina_producto($linea){
$this->array_id_prod[$linea]=0;
}
}
//inicio la sesión
session_start();
//si no esta creado el objeto carrito en la sesion, lo creo
if (!isset($_SESSION["ocarrito"])){
$_SESSION["ocarrito"] = new carrito();
}
?>
y esto me muestra el carrito
ver_carrito.php
Código PHP:
<?
include("lib_carrito.php");
?>
<html>
<head>
<title>Introduce Producto</title>
</head>
<body>
<?
$_SESSION["ocarrito"]->imprime_carrito();
?>
<br>
<br>
<a href="index.php">Volver</a>
</body>
</html>