Code: Select all
function cancel()
{
$file = file("carts.txt"); // read carts.txt file into an array called $file. the info in this array is serialized
foreach ($file as $line) //read each line of the array into $line
{
$my_line = trim($line); // take out the white space
$current_reservations = unserialize($my_line); // unserialize the string and get the the stored carts array
if (!($current_reservations['name']==$cart_array['name'] && $current_reservations['date']==$cart_array['date']
&& $current_reservations['cart']==$cart_array['cart']))//find cart info that does not match what the user input
{
$new_reservations[] = $current_reservations; //read everything that is not what the user wanted cancelled into a new array
//now it is time to write this new_array to the carts.txt file
$fp = fopen("carts.txt", "w"); //open carts.txt file make it writable
if ($fp)
{
foreach ($new_reservations as $carts)
{
$my_carts = serialize($carts); //serialize the carts
fwrite($fp,$my_carts);
echo "you have successfully cancelled your cart reservation";
}
}
else
{
echo "was not able to write to carts.txt file";
}
}
else
{
echo "You do not have this cart checked out on this particular date";
}
}
}