Productos.txt
Codigo_Producto_1,Cantidad_Producto_1,Codigo_Produ cto_4,Cantidad_Producto_4,Codigo_Producto_2,Cantid ad_Producto_2,
los precios van en un archivo aparte
Precios.txt
Precio_Producto_1,Precio_Producto_4,Precio_Product o_2,
Este codigo muestra el carrito en pantalla
Código PHP:
<form action="eliminar.php" method="post">
<?
if (file_exists("Productos.txt"))
{
$texto=file_get_contents("Productos.txt");
$array=explode(",",$texto);
$texto=file_get_contents("Precios.txt");
$array2=explode(",",$texto);
$n=sizeof($array) - 1;
$acum=0;
for($i=0;$i<$n/2;$i++)
{
$s=2*$i;
echo $array[2*$i];
echo "({$array[(2*$i)+1]}):";
echo "$";
echo $array[(2*$i)+1]*$array2[$i];
$acum=$acum+$array[(2*$i)+1]*$array2[$i];
echo "<input name='prod' value='$array[$s]' type='text'>";
echo "<input type='submit'>";
echo "<br>";
echo "<br>";
}
echo "Total: $";
echo $acum;
}
else { echo "Su carrito está vacío.";}
?>
</form>
Código PHP:
<html>
<head>
<title>Eliminando</title>
</head>
<body>
<?php
echo $_POST['prod'];
$texto=file_get_contents("Productos.txt");
$array=explode(",",$texto);
$n=sizeof($array) - 1;
$texto=file_get_contents("Precios.txt");
$precios=explode(",",$texto);
for($i=0;$i<($n-2);$i++)
{
$array_nuevo[$i] = "0";
}
for($i=0;$i<($n-2)/2;$i++)
{
$precios_nuevo[$i] = "0";
}
for($i=0;$i<$n/2;$i++)
{
if($array[2*$i]!=$_POST["prod"])
{
$array_nuevo[2*$i]=$array[2*$i];
$array_nuevo[(2*$i)+1]=$array[(2*$i)+1];
$precios_nuevo[$i]=$precios[$i];
}
}
unlink("Productos.txt");
$ar=fopen("Productos.txt","a");
for($i=0;$i<($n-2);$i++)
{
fputs($ar,$array_nuevo[$i]);
fputs($ar,",");
}
fclose($ar);
unlink("Precios.txt");
$ar=fopen("Precios.txt","a");
for($i=0;$i<($n-2)/2;$i++)
{
fputs($ar,$precios_nuevo[$i]);
fputs($ar,",");
}
fclose($ar);
echo "Eliminacion exitosa.";
?>
<br><a href="Shop.htm"> Volver </a>
</body>
</html>
Código PHP:
echo "<input name='prod' value='$array[$s]' type='text'>";
muchas gracias!!
EDIT: Ahora que lo pienso, el for que se ejecuta dentro de mostrar carrito es para un unico formulario (el declarado afuera del bucle), sera eso??