carrito.php
Código:
addproduct.php<?php class carrito { var $num_productos; var $array_id; var $array_nombre; var $array_descripcion; var $array_tamano; var $array_precio; var $array_cantidad; function __construct() { $this -> num_productos = 0; } function introduce_producto($id, $nombre,$descripcion ,$tamano, $precio) { if (!in_array($id, $this -> array_id)) { $this -> array_id[$this -> num_productos] = $id; $this -> array_nombre[$this -> num_productos] = $nombre; $this -> array_descripcion[$this -> num_productos] = $descripcion; $this -> array_tamano[$this -> num_productos] = $tamano; $this -> array_precio[$this -> num_productos] = $precio; $this -> array_cantidad[$this -> num_productos] = 1; $this -> num_productos++; } else { $posicion = array_search($id, $this -> array_id); $this -> array_cantidad[$posicion]++; } } function elimina_producto($linea) { $this -> array_id[$linea] = 0; } function imprime_carrito() { $suma = 0; for ($i = 0; $i < $this -> num_productos; $i++) { if ($this -> tamano[$i] == "mini") { echo '<li id="' . $i . '"><a href="products.php?id=' . $this -> array_id[$i] . '&tamano=mini" class="item"><img src="/images/canastillas/' . $this -> array_id[$i] . '"mini.jpg" /></a> <h4>' . $this -> array_nombre[$i] . '</h4> <p>'.$this -> array_descripcion[$i].'</p> <h5>Unidades: ' . $this -> array_cantidad[$i] . ' </h5> <h3>' . round($this -> array_cantidad[$i] * $this -> array_precio[$i], 2) . '€</h3> <a class="delete" href="eliminar_producto_carrito.php?id=' . $this -> array_id[$i] . '">Eliminar</a></li>'; } if ($this -> tamano[$i] == "midi") { echo '<li id="' . $i . '"><a href="products.php?id=' . $this -> array_id[$i] . '&tamano=midi" class="item"><img src="/images/canastillas/' . $this -> array_id[$i] . '"midi.jpg" /></a> <h4>' . $this -> array_nombre[$i] . '</h4> <p><p>'.$this -> array_descripcion[$i].'</p></p> <h5>Unidades: ' . $this -> array_cantidad[$i] . ' </h5> <h3>' . round($this -> array_cantidad[$i] * $this -> array_precio[$i], 2) . '€</h3> <a class="delete" href="eliminar_producto_carrito.php?id=' . $this -> array_id[$i] . '">Eliminar</a></li>'; } if ($this -> tamano[$i] == "maxi") { echo '<li id="' . $i . '"><a href="products.php?id=' . $this -> array_id[$i] . '&tamano=maxi" class="item"><img src="/images/canastillas/' . $this -> array_id[$i] . '"maxi.jpg" /></a> <h4>' . $this -> array_nombre[$i] . '</h4> <p><p>'.$this -> array_descripcion[$i].'</p></p> <h5>Unidades: ' . $this -> array_cantidad[$i] . ' </h5> <h3>' . round($this -> array_cantidad[$i] * $this -> array_precio[$i], 2) . '€</h3> <a class="delete" href="eliminar_producto_carrito.php?id=' . $this -> array_id[$i] . '">Eliminar</a></li>'; } if ($this -> tamano[$i] == "producto") { echo '<li id="' . $i . '"><a href="mum.php" class="item"><img src="/images/products/' . $this -> array_id[$i] . '".jpg" /></a> <h4>' . $this -> array_nombre[$i] . '</h4> <p><p>'.$this -> array_descripcion[$i].'</p></p> <h5>Unidades: ' . $this -> array_cantidad[$i] . ' </h5> <h3>' . round($this -> array_cantidad[$i] * $this -> array_precio[$i], 2) . '€</h3> <a class="delete" href="eliminar_producto_carrito.php?id=' . $this -> array_id[$i] . '">Eliminar</a></li>'; } } } function precioTotalCarrito() { $preciototal = 0; for ($i = 0; $i < $this -> num_productos; $i++) { $preciototal = $preciototal + ($this -> array_precio * $this -> array_cantidad); } return round($preciototal, 2); } } ?>
Código:
Como veis el método se llama igual tanto en la llamada como en la clase. La conexión a la BD funciona correctamente así como la creación del objeto carrito en la variable de sesión (comprobado mediante un var_dump()).<?php require_once ("carrito.php"); require_once ("operacionesbd.php"); require_once ('crearconexionbd.php'); crearConexionBD(); session_start(); $id = $_GET["id"]; $tipo = $_GET["type"]; if ($tipo == "producto") { $elemento = mysql_fetch_assoc(seleccionaCanastillaProductoPorID($id, $tipo)); $descripcion = substr($elemento["descripcion"], 0, 70); $descripcion = $descripcion . "..."; $_SESSION["carrito"]::introduce_producto($elemento["idproducto"], $elemento["nombre"], $descripcion, $tipo, $elemento["precio"]); } else { $elemento = mysql_fetch_assoc(seleccionaCanastillaProductoPorID($id, $tipo)); $descripcion = substr($elemento["descripcion"], 0, 70); $descripcion = $descripcion . "..."; if ($tipo == "mini") { $precio = $elemento["preciomini"]; } if ($tipo == "midi") { $precio = $elemento["preciomidi"]; } if ($tipo == "maxi") { $precio = $elemento["preciomaxi"]; } $_SESSION["carrito"] -> introduce_producto($elemento["idcanastilla"], $elemento["nombre"], $descripcion, $tipo, $elemento["precio"]); } ?>
Gracias de antemano, un saludo.