Estoy haciendo un carrito basandome en el tutorial de desarrolloweb
La cosa extraña que hace es que al insertar un producto en la sesion del carrito
no lo inserta, si antes no has ido a la libreria del carrito. Os voy a poner los codigos de la libreria y del archivo que inserta en la sesion que son los problematicos.
Libreria
Código PHP:
<?
class carrito {
//atributos de la clase
var $num_productos;
var $array_id_prod;
var $array_nombre_prod;
var $array_precio_prod;
var $array_tarifa;
//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,$ref,$nombre_prod,$precio_prod,$tarifa){
$this->array_id_prod[$this->num_productos]=$id_prod;
$this->array_ref[$this->num_productos]=$ref;
$this->array_nombre_prod[$this->num_productos]=$nombre_prod;
$this->array_precio_prod[$this->num_productos]=$precio_prod;
$this->array_tarifa[$this->num_productos]=$tarifa;
$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 width="530" border=1 bordercolor="#000000" cellspacing="0" cellpadding="0" class=\"txt\">
<tr>
<td align="center" class="txtprensa"><b> Referencia </b></td>
<td align="center" class="txtprensa"><b> Descripcion </b></td>
<td align="center" class="txtprensa"><b> Precio </b></td>
<td align="center" class="txtprensa"><b> Tarifa transporte </b></td>
<td align="center" class="txtprensa"> </td>
</tr>';
for ($i=0;$i<$this->num_productos;$i++){
if($this->array_id_prod[$i]!=0){
echo '<tr>';
echo "<td class=\"txt\" align=\"center\">" . $this->array_ref[$i] . "</td>";
echo "<td class=\"txt\">" . $this->array_nombre_prod[$i] . "</td>";
echo "<td class=\"txt\" align=\"right\">" . $this->array_precio_prod[$i] . " €</td>";
echo "<td class=\"txt\" align=\"right\">".$this->array_tarifa[$i]." </td>";
echo "<td class=\"txt\" align=\"center\"><a class=\"txt\" href='del_prod.php?linea=$i'>Eliminar</a></td>";
echo '</tr>';
$suma += $this->array_precio_prod[$i];
$suma2 += $this->array_tarifa[$i];
$subtotal= $suma + $suma2;
$iva= $suma * 0.16;
$total= $suma + $iva;
}
}
echo "<tr><td> </td><td> </td><td> </td><td> </td><td> </td></tr>";
//muestro el total
echo "<tr><td> </td><td> </td> <td> </td><td class=\"txt\" align=\"right\"><b>Subtotal:</b></td><td class=\"txt\" align=\"right\"> <b> $subtotal € </b></td></tr>";
//IVA
echo "<tr><td> </td><td> </td><td> </td><td class=\"txt\" align=\"right\"><b>IVA (16%):</b></td><td class=\"txt\" align=\"right\"> <b> " . $iva . " € </b></td></tr>";
echo "<tr><td> </td><td> </td><td> </td><td class=\"txt\" align=\"right\"><b>Total:</b></td><td class=\"txt\" align=\"right\"> <b> " . $total . " € </b></td></tr>";
echo "</table>";
echo "<a class=\"txt\" href=\"del_prod.php?ds=1\">Vaciar</a>";
}
//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["carrito"])){
$_SESSION["carrito"] = new carrito();
}
?>
Código PHP:
<?
include("lib_carrito.php");
include("conf.php");
session_start();
if (!isset($_SESSION["carrito"])){
$_SESSION["carrito"] = new carrito();
}
if($anf != ''){
$ref=mysql_query("SELECT * FROM anforas WHERE Referencia=\"$anf\"");
if($con = mysql_fetch_array($ref)){
$_SESSION["carrito"]->introduce_producto(1,$con[Referencia], $con[Descripcion],$con[Precio],$con[Tarifa_transporte]);
}}
if($ch2 != ''){
$acc=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch2\"");
if($con2 = mysql_fetch_array($acc)){
$_SESSION["carrito"]->introduce_producto(1,$con2[Referencia], $con2[Descripcion],$con2[Precio],$con2[Tarifa_transporte]);
}}
if($ch3 != ''){
$acc2=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch3\"");
if($con3 = mysql_fetch_array($acc2)){
$_SESSION["carrito"]->introduce_producto(1,$con3[Referencia], $con3[Descripcion],$con3[Precio],$con3[Tarifa_transporte]);
}}
if($ch4 != ''){
$acc3=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch4\"");
if($con4 = mysql_fetch_array($acc3)){
$_SESSION["carrito"]->introduce_producto(1,$con4[Referencia], $con4[Descripcion],$con4[Precio],$con4[Tarifa_transporte]);
}}
if($ch5 != ''){
$acc4=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch5\"");
if($con5 = mysql_fetch_array($acc4)){
$_SESSION["carrito"]->introduce_producto(1,$con5[Referencia], $con5[Descripcion],$con5[Precio],$con5[Tarifa_transporte]);
}}
if($ch6 != ''){
$acc5=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch6\"");
if($con6 = mysql_fetch_array($acc5)){
$_SESSION["carrito"]->introduce_producto(1,$con6[Referencia], $con6[Descripcion],$con6[Precio],$con6[Tarifa_transporte]);
}}
if($ch7 != ''){
$acc6=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch7\"");
if($con7 = mysql_fetch_array($acc6)){
$_SESSION["carrito"]->introduce_producto(1,$con7[Referencia], $con7[Descripcion],$con7[Precio],$con7[Tarifa_transporte]);
}}
if($ch8 != ''){
$acc7=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch8\"");
if($con8 = mysql_fetch_array($acc7)){
$_SESSION["carrito"]->introduce_producto(1,$con8[Referencia], $con8[Descripcion],$con8[Precio],$con8[Tarifa_transporte]);
}}
if($ch9 != ''){
$acc8=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch9\"");
if($con9 = mysql_fetch_array($acc8)){
$_SESSION["carrito"]->introduce_producto(1,$con9[Referencia], $con9[Descripcion],$con9[Precio],$con9[Tarifa_transporte]);
}}
if($ch10 != ''){
$acc9=mysql_query("SELECT * FROM accesorios WHERE Referencia=\"$ch10\"");
if($con10 = mysql_fetch_array($acc9)){
$_SESSION["carrito"]->introduce_producto(1,$con10[Referencia], $con10[Descripcion],$con10[Precio],$con10[Tarifa_transporte]);
}}
echo '<html>
<head>
<title>Introduce Producto</title>
</head>
<body>
<table border="0" cellspacing="1" cellpadding="0" align="center">
<tr align="center" valign="bottom">
<td height="108" colspan="2" class="txt">Los objetos se han añadido correctamente a su pedido.<br />
<br />
</td>
</tr>
<tr >
<td height="3" colspan="2" bgcolor="#000000"></td>
</tr>
<tr>
<td width="169" align="left" class="txt"><a href="index.php?sec=3&ss=1">Volver a ánforas</a> </td>
<td width="169" align="right" class="txt"><a href="index.php?sec=3&ss=1&car=1">Realizar pedido</a> </td>
</tr>
</table>
</body>
</html>
';
?>
N3m0