/lib_carrito.php
Código PHP:
   <?PHP
 
//variable de session
if (!isset($_SESSION["ocarrito"])){ 
       $_SESSION["ocarrito"] = new carrito();
}
 
class carrito { 
       //atributos de la clase 
   var  $num_productos; 
   var  $array_id_prod; 
   var  $array_nombre_prod; 
   var  $array_precio_prod; 
}
///////// INTRODUCIR PRODUCTO FUCNCION////////////////
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++; 
}
////////////////////////////////////
/////////BORRAR PRODUCTO FUNCION////////////////
function elimina_producto ($linea){ 
    $this ->array_id_prod[$linea]=0;
    }
///////////////////////////////////////////////
///////////mostrar todos los productos del carrito/////////
function imprime_carrito(){ 
       $suma = 0; //guarda importe del prod
       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>"; 
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>";
}
}
?>    Código PHP:
   <?PHP 
include("lib_carrito.php"); 
?> 
 
<html> 
<head> 
       <title>Trabajando con el carrito</title> 
</head> 
 
<body> 
 
 
<a href="mete_producto.php?id=12&nombre=silla+modelo+jupiter&precio=80">Silla modelo jupiter 80 euros</a> 
<br> 
<br> 
<a href="mete_producto.php?id=16&nombre=mesa+camilla+oferta&precio=29">Mesa camilla en oferta 29 euros</a> 
<br> 
<br> 
<a href="mete_producto.php?id=18&nombre=Silla+oficina&precio=102">Silla oficina 102 euros</a> 
<br> 
<br> 
<a href="mete_producto.php?id=98&nombre=Aparador+cocina+blanco&precio=200">Aparador cocina blanco 200 euros</a> 
<br> 
<br> 
<br> 
- <a href="ver_carrito.php">Ver carrito</a> 
</body> 
</html>   Código PHP:
   <?PHP
include("lib_carrito.php"); 
$_SESSION["ocarrito"]-> introduce_producto($_get["id"], $_get["nombre"], $_get["precio"]); 
 
 
?> 
<html> 
<head> 
       <title>Introduce Producto</title> 
</head> 
<body> 
 
Producto introducido. 
<br> 
<br> 
<a href="index.php">- Volver</a> 
<br> 
                                                                                                                     <br> 
<a href="ver_carrito.php">- Ver carrito</a> 
 
</body> 
</html>   Fatal error: Call to undefined method carrito::introduce_producto() in D:\appserver\AppServ\www\tvirtual2\mete_producto.p hp on line 3
 


