Tenemos un documento productos.php que muestra los productos. Si no llamamos a la libreria del carrito, todo funciona segun lo esperado:
Código PHP:
<?php
require("inc/funciones.php");
if ($HTTP_SERVER_VARS["REQUEST_METHOD"]=="GET")
{
$idcategoria=$_GET['idcategoria'];
$idcategoriamenu=$_GET['idcategoriamenu'];
}
else
{
$idcategoria=$_POST['idcategoria'];
$idcategoriamenu=$_POST['idcategoriamenu'];
}
if (maliciosa($idcategoria)) //si detectamos caracter extraño, no mostramos la pagina y vamos al indice
{
exit(0);
//$redirect = "Location: index.htm";
//header($redirect);
}
else //si no hay nada extraño, continuamos
{
require("inc/conexion.php");
$link=conectarse();
[COLOR="Red"]include("inc/lib_carrito_new.php");[/COLOR]
?>
<html>
<head>
<title>mkgarena.com - Consultores de marketing deportivo para recintos deportivos</title>
<?php require("inc/head.php"); ?>
</head>
<body leftmargin="10" marginwidth="10" marginheight="30" topmargin="30" bgcolor="#000000">
<table border="0" cellspacing="0" cellpadding="0" width="975">
<tr>
<td width="350" valign="top">
<?php require("inc/opciones-tienda.php"); ?>
</td>
<td valign="top">
<?php require("inc/menu-superior-tienda.php"); ?><br><br>
<?php
$sql="select pr.idpresentacion,pr.nombre as presentacion,pr.caracteristica,pr.precio,e.nombre as entidad,p.idproducto,p.nombre as producto,p.foto,p.descripcion,c.nombre as categoria ";
$sql .= "from productos p, entidades e, categorias c,presentaciones pr ";
$sql .= "where pr.idproducto=p.idproducto and c.idcategoria=p.idcategoria and p.identidad=e.identidad and p.idcategoria=" . $idcategoria;
$sql .= " order by p.orden,pr.orden";
$result=mysql_query($sql,$link);
$row=mysql_fetch_array($result);
$num_elem=mysql_num_rows($result);
?>
<table width="90%" align="center" border="0" cellpadding="0" cellspacing="0">
<tr><td align="center" class="blanco-neg-big"><?php echo $row["categoria"]; ?><br><br></td></tr>
</table>
<table width="90%" align="center" border="0" cellpadding="0" cellspacing="0">
<tr><td align="center" class="amarillo-neg-med">PRODUCTO OFICIAL <?php echo $row["entidad"]; ?><br><br></td></tr>
</table>
<br><br>
<table bgcolor="#ffffff" width="90%" align="center" border="0" cellpadding="0" cellspacing="0">
<tr><td colspan="5">
<table height="6" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr valign="top">
<td height="6" width="6"><img src="../img/esq_sup_izq.gif"></td>
<td height="6" > </td>
<td height="6" width="6"><img src="../img/esq_sup_dch.gif"></td>
</tr>
</table>
</td></tr>
<tr align="center" class="infoBoxHeading">
<td colspan="2"> </td>
<td>Producto</td>
<td>Modelo</td>
<td>Precio</td>
</tr>
<?php
do
{
echo $num_elem;
?>
<tr>
<td width="170" align="center">
<a href="<?php echo "producto.php?idcategoria=" . $idcategoria . "&idpresentacion=" . $row["idpresentacion"] . "&idcategoriamenu=" . $idcategoriamenu; ?>">
<img border="0" src="../img/<?php echo foto_peq($row["foto"]); ?>" alt="<?php echo $row["producto"]; ?>">
</a>
</td>
<td width="30" align="center" valign="bottom">
<a class="txt-negro-neg" href="javascript:muestra_foto('<?php echo $row["foto"]; ?>','<?php echo $row["presentacion"] ?>')">
<img align="bottom" border="0" src="../img/lupa_zoom.gif" alt="zoom">ZOOM
</a>
</td>
<td width="310" class="txt-negro-neg" align="center"><?php echo $row["presentacion"]; ?></td>
<td width="150" class="txt-negro-neg" align="center"><?php echo $row["caracteristica"]; ?></td>
<td width="150" class="txt-negro-neg" align="center"><?php echo $row["precio"]; ?> €</td>
</tr>
<?php if ($num_elem!=1) { ?>
<tr><td align="center" valign="middle" colspan="5"><img height="1" width="550" src="../img/linea_negra.jpg"></td></tr>
<?php
}
$num_elem--;
}
while($row=mysql_fetch_array($result))
?>
</table>
<table bgcolor="#ffffff" width="90%" align="center" border="0" cellpadding="0" cellspacing="0">
<tr><td>
<table height="6" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr valign="top">
<td background="../img/esq_inf_izq.gif" height="6" width="6"></td>
<td> <img width="200" height="1" src="../img/blanco1px.gif"></td>
<td background="../img/esq_inf_dch.gif" height="6" width="6"></td>
</tr>
</table>
</td></tr>
</table>
</td>
</tr>
</table>
<?php
mysql_free_result($result);
mysql_close($link);
}
?>
</body>
</html>
Ahora os copio la clase carrito (que esta en este fichero):
Código PHP:
<?php
class carrito {
//atributos de la clase
var $num_productos;
var $array_id_prod;
var $array_cantidad_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,$cantidad,$precio_prod){
$this->array_id_prod[$this->num_productos]=$id_prod;
$this->array_nombre_prod[$this->num_productos]=$nombre_prod;
$this->array_cantidad_prod[$this->num_productos]=$cantidad;
$this->array_precio_prod[$this->num_productos]=($precio_prod*$cantidad);
$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_cantidad_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 (iva incluido):</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 "</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
[COLOR="Red"]session_start();[/COLOR]
//si no esta creado el objeto carrito en la sesion, lo creo
if (!isset($_SESSION["ocarrito"])){
$_SESSION["ocarrito"] = new carrito();
}
?>
¿Alguno de vosotros podría darme una solución? Muchísimas gracias