Page 1 of 1

Please help, I am so close

Posted: Sat Nov 04, 2006 7:25 pm
by dagee
As some people here know, I have been working on this one program for work for a while. For the most part it is
going well (thanks to some really helpful people here :) ). Right now, I feel that I am really close to a part that has been really giving me grief. I have been trying to work with a great example a poster gave me - viewtopic.php?p=323007&highlight=#323007. I have made some small changes to this as I have tried to understand exactly what is going on with his code.

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>
feyd | This is the last time I remind you.
[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.

Posted: Sat Nov 04, 2006 9:16 pm
by printf
I tried to explain to you the problem you are having, but your just not wanting to understand. Again, you need to read the PHP manual so you understand exactly what each function does, so that you change your application format to one that the function supports!

Now lets go through this, one last time!

Code: Select all

$file = file("carts.txt");
The function file(), opens a file and reads that file into array. Each array element will be a single line, determined by the system end of line character(s)! So a file like the example below.

Code: Select all

line 1
line 2
line 3
(empty line)
Would return a file() array, like the example below...

Code: Select all

Array (
	0 = line 1,
	1 = line 2,
	2 = line 3,
	3 = 
)
Each array element (key => value), will contain a value containing the line contents followed by the end of line character(s)! So you must use rtrim() to remove the end of line character(s)!



Now, when you write to your file (carts.txt), your not adding the end of line character(s)!, so when you write to the file and don't add the end of line character(s), it ends up looking like below!

Code: Select all

line 1line 2line 3

So when you read thefile(), you end up with file() array like below...

Code: Select all

Array (
	0 = line 1line 2line 3
)
Which is not in the right format that your application needs. So everything you do, will also fail!


printf

Thank you printf

Posted: Sat Nov 04, 2006 9:42 pm
by dagee
That makes sense. When you last posted to my previous post your post was just too far beyond me. I was looking at a couple of similar posts dealing with similar issues and the way they were handled mades sense to me. Your's while I am sure was really good was like I said was beyond me. Being new to this whole programming thing I am trying to solidify what I know and use that small bit of knowledge to write programs (and slowly build up my reportaire). One of the issues I have been having conceptually is the array idea. For some reason, I don't know why, I have had some real conceptual problems with arrays.

Your explanation makes a lot of sense and I think clarifies one of the conceptual issues I was having, at least theoretically. By the way, the idea of read the manual and it will all make sense may work for some but definitely not me. While I go through the various links on this site and read the written explanations to the various code pieces often times they don't really sink in. It takes some actual trial and error to really go through and have it sink in.

In the future though, I will definitely be more thorough in my reseach and try not to be so thick.

Dag

Posted: Sat Nov 04, 2006 10:52 pm
by Cameri
Well, It was my who forgot to add the new line character at the end when saving ^^!

Code: Select all

<?php

ini_set('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE); //<-- if you are learning, that's bad, you should be getting any notices if you are doing everything correctly!

//Get the information from CartCheckoutInput.html and put it into variables

// These variables are not being validated, don't trust user input!
$name = trim($_POST['name']);
$date = trim($_POST['date']);
$cart = trim($_POST['cart']);
$reservation = trim($_POST['reservation']);

echo $name. "<br />";//there is no need to include the variables to echo inside double quotes, it's an overkill
echo $date."<br />";
echo $cart."<br />";
//echo "$reservation"."<br />";//testing variables

//store $name,$cart,date into an array called $cart_array
/* //Useless
$cart_array = array(
        'name' => $name,
        'date' => $date,
        'cart' => $cart
);
*/

$file = file("carts.txt"); //get text file and save as an array.
//NOW, each line consists of a serialized array, followed by an end-of-line (a.k.a. EOL) character.
$new_carts = array();
foreach ($file as $line)
{
        $my_line = rtrim($line);//remove the EOL character at the end
        $current_cart = unserialize($my_line); //unserialize
        echo "<br />";
        if (strcmp($reservation,'cancel')==0)
        {
               // here we just add to the array of carts the carts that DO NOT match the criteria, that is, all the other carts that are not being canceled!
                if (!(strcmp($name,$current_cart['name'])==0 && strcmp($date,$current_cart['date'])==0 && strcmp($cart,$current_cart['cart'])==0))
                $new_carts[] = $current_cart;
         }
}

// Saving part below, do not mix it with the part above!
$fp = fopen("carts.txt", "w");
if ($fp)
{
         // walk through the $new_carts array, well they are not really new, they are just the rest of the carts, except the one you wished to remove
         foreach ($new_carts as $carts){
                //the fixed way of writing it back to the file, the serialized array, plus EOL
                $write_carts = serialize($carts).PHP_EOL; //<-- php's end-of-line (consists of \r followed by \n)
                 //\r means return carriage, and \n new line.
                fwrite($fp,$write_carts);
         }
          fclose($fp); //<-- no need to put it ouside the block.. since it only has to close the file if it was able to open it in the first place
}

?>

Thanks Cameri

Posted: Sat Nov 04, 2006 11:37 pm
by dagee
Thank you Cameri especially for all the comments in the code you made. I am still going through it (don't quite understand it all yet but your comments were really helpful).