Need some guidance please
Posted: Tue Oct 31, 2006 9:28 pm
Ok, I have posted on this topic before and have gotten a lot of really helpful responses. Unfortunately I am still stuck and I don't know why. Background for those who are not familiar with the project I am working on. For my work (I am a teacher) teachers can check out computer carts. So far I have written a program (with lots of help from people here) that allows teachers to reserve a computer cart. What I want to do now is write a function that allows teachers to cancel a reservation. This is what I have so far. By the way this is not all my own ideas. A person from this forum was good enough to give som guidance with this unfortunately I'm still not getting this to work. The problem is that it is not actually changing the carts.txt file. For the life of me I cannot see why. Please, what am I missing. There is a flaw in my logic (ok probably more than one flaw).
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";
}
}
}