going well (thanks to some really helpful people here
In a nutshell, my program goes through a text file and gets rid of pieces that match a certain criteria. Right now, this section of code does exactly what I want, it is rewriting the text file as an array with the proper inforamtion gone. The only problem I am having is that when I go to write to the text file (carts.txt) it erases and what it leaves in garbage.
How do I modify the writing to the carts.txt file so that it writes correctly?
Code: Select all
<?php
ini_set('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
//Get the information from CartCheckoutInput.html and put it into variables
$name = trim($_POST['name']);
$date = trim($_POST['date']);
$cart = trim($_POST['cart']);
$reservation = trim($_POST['reservation']);
echo "$name". "<br />";//testing variables
echo "$date"."<br />";//testing variables
echo "$cart"."<br />";//testing variables
//echo "$reservation"."<br />";//testing variables
//store $name,$cart,date into an array called $cart_array
$cart_array = array(
'name' => $name,
'date' => $date,
'cart' => $cart
);
$file = file("carts.txt"); //get text file and save as an array. The array is serialzied
foreach ($file as $line)
{
$my_line = trim($line);//remove white spaces
$current_cart = unserialize($my_line);//unserialize
//echo $current_cart['name']; here for testing purposes
echo "<br />";
if ($reservation == 'cancel')
{
//find carts that do not meet the below criteria
if (!($cart_array['name']==$current_cart['name'] && $cart_array['date']==$current_cart['date'] && $cart_array['cart']==$current_cart['cart']))
{
//echo $current_cart['name']; this is just here for testing purposes
$new_cart = $current_cart;//this is a array of carts.txt without the cancelled cart
//Write $new_cart array to the carts.txt file. Will need to unserialize.
//Here is the problem ****************************************************************************
$fp = fopen("carts.txt", "w");
if ($fp)
{
foreach ($new_cart as $carts) //go through array line by line
{
$write_carts = serialize($carts);//serialize in preperation for writing to carts.txt file
fwrite($fp,$write_carts);
}
}
fclose($fp);
//Problem end ************************************************************************************
}
}
//echo $cart_array['name'];//testing array elements
//echo $cart_array['date'];//testing array elements
//echo $cart_array['cart'];//testing array elements
}
?>
</body>
</html>[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.