Page 1 of 1

Need some guidance please

Posted: Tue Oct 31, 2006 9:28 pm
by dagee
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";
	 }
  }
  
}

Posted: Tue Oct 31, 2006 9:45 pm
by printf
Your not adding a system related line ending when writing to the carts.txt file, so when you read from the file you reading the complete file as a sigle array element instead of having a single $current_reservations!

On linux

Code: Select all

fwrite ( $fp, $my_carts . "\n" );
on windows

Code: Select all

fwrite ( $fp, $my_carts . "\r\n" );
on OS mac

Code: Select all

fwrite ( $fp, $my_carts . "\r" );

Also when you read the file, use array_map, surrounded by array_dff, so you trim and remove the last empty element in the file array!

so this....

Code: Select all

$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
could be changed to

Code: Select all

$file = array_diff ( array_map ( 'rtrim', file ( 'carts.txt' ) ), array ( '' ) );

foreach ( $file AS $line )
{

printf

Posted: Tue Oct 31, 2006 10:44 pm
by dagee
First off, thank you so much for your help. I still am having some problems but first I am a bit confused by this:
$file = array_diff ( array_map ( 'rtrim', file ( 'carts.txt' ) ), array ( '' ) ); What exactly is this line doing? There is just so
much going on here that I cannot follow.

Second, for some reason this line is not working
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

when I enter in the identical information it cannot tell they are the same and is insisting that they are different. I tried to have the program echo each of the pieces being compared but it would not (which really confuses me - because I thought I had the whole echo thing down.) So what is going on here?

Code: Select all

function cancel()
{
	$file = array_diff ( array_map ( 'rtrim', file ( 'carts.txt' ) ), array ( '' ) ); // <--- I am lost here??

	echo $current_reservations['name'];//testing  <----this is not printing
	echo $cart_array['name'];//testing <----this is not printing
	echo $cart_array['date'];//testing <----this is not printing

	$current_reservations['date'];//testing 
	foreach ( $file as $line ) 
	{
	$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 . "\r" );  
			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";
	 }
  }
  
}