Page 1 of 1

Arg I'm stuck

Posted: Tue Oct 24, 2006 8:48 pm
by dagee
Ok, I'm working on this project for my work (same project I have been working on all week for those who have gernerously helped me out so far).

Here is what is going on. I have written a program that allows teachers (I work at a school) to check out computer cars. The problem is that I am having trouble writing the section of code that deals with cart resrevation cancellation.

Here is what I have so far (I think I am really close but it just is not working): The problem is that currently when a person cancels a cart the program completely erases the carts.txt file. I have a couple of theories as to why but don't really have any idea of where to go from here.

Again, thank you so much for your help and patience as I very slowly and painfully work through my newbie phase.

Code: Select all

$name = trim($_POST['name']); 
$cart = trim($_POST['cart']);  
$date = trim($_POST['date']);
$reservation = trim($_POST['reservation']); //outputs can only be 'cancel' or 'reserve'

$cart_array = array( 
  'name'  => $name, 
  'cart'  => $cart,  
  'date' => $date,
  'reservation' => $reservation 
); 

if($reservation == "cancel")// checking to see if user is canceling reservation
{
	$eraseData = array(
	'name' => "free",
	'cart' => "free",
	'date' => "free"
	);
	$file = file("carts.txt"); //get the file as an array
	foreach($file as $line)
	{
		$myline = trim($line); //remove any spaces
		$current_cart = unserialize($myline); //unserialize the string and get the stored cart array
		
		$switch_text_file = str_replace($current_cart,$eraseData,$current_cart); //make the replacement
		
		//open text file and switch out reservation info with $eraseData
		$temp_string = trim($switch_text_file);
		$cart_string = serialize($temp_string);
		$fp = fopen("carts.txt","w");
		fwrite($fp,$cart_string);
		fclose($fp);
	}
}  

Re: Arg I'm stuck

Posted: Tue Oct 24, 2006 9:12 pm
by volka
Please try to explain step-by-step your foreach($file as $line) loop.
Not want you want it to do but what your code is doing, step-by-step ;)

Posted: Tue Oct 24, 2006 9:59 pm
by Cameri
Try this (untested):

Code: Select all

$name = trim($_POST['name']);
$cart = trim($_POST['cart']); 
$date = trim($_POST['date']);
$reservation = trim($_POST['reservation']); //outputs can only be 'cancel' or 'reserve'

if($reservation == "cancel")// checking to see if user is canceling reservation
{
	$file = file("carts.txt"); //get the file as an array
    if ($file !== false) {
    	$carts = array();
        foreach($file as $line)
        {
        	$line = trim($line);
        	$cart = unserialize($line);
        	// only keep carts that don't match the following conditions
	       	if (!($cart['name']==$name && $cart['cart']==$cart && $cart['date']==$date))
				$carts[] = $cart;
        }
		//now we got the array of carts without the  
		$fp = fopen("carts.txt","w");
		if ($fp) {
			foreach($carts as $cart){
				$cart_string = serialize($cart);
				fwrite($fp,$cart_string);
			}
        	fclose($fp);
        } else {
			echo "Couldn't write in carts.txt!\n";
		}
	} else {
		echo "Couldn't open carts.txt for reading!\n";
	}
}

Posted: Tue Oct 24, 2006 10:16 pm
by dagee
Ok, I think I see part of it. It is really hard to seperate what the code is acually saying and what I am hoping it is saying.

Code: Select all

$name = trim($_POST['name']); 
$cart = trim($_POST['cart']);  
$date = trim($_POST['date']); 
$reservation = trim($_POST['reservation']); //outputs can only be 'cancel' or 'reserve' 

$cart_array = array( 
  'name'  => $name, 
  'cart'  => $cart,  
  'date' => $date, 
  'reservation' => $reservation 
); 

if($reservation == "cancel")// checking to see if user is canceling reservation 
{ 
        $eraseData = array( 
        'name' => "free", 
        'cart' => "free", 
        'date' => "free" 
        ); 
        $file = file("carts.txt"); //get the file as an array 
        foreach($file as $line) 
        { 
                $myline = trim($line); //Here I am removing any spaces 
                $current_cart = unserialize($myline); //unserialize the string and get the stored cart array 
                
                $switch_text_file = str_replace($current_cart,$eraseData,$current_cart); //here I am replaceing the current text data with the eraseData array.//this is not working so something is definitely wrong here.  Everything should be repalced with the word "free" but it is not.  I really think it has to do with the array that I am changing.  I probably should not be changing $current_cart but if I don't change that what do I change?  Can I change $file even though it is serialized.  Should I unserialize that file first before sending though the foreach loop?
                $temp_string = trim($switch_text_file); //making sure there are now white splaces
                $cart_string = serialize($temp_string); //serializing in preperation for writing to the text file.
                $fp = fopen("carts.txt","w"); /*Here is probably one issue.  I am writing to the carts.txt file each time though the foreach loops iteneration.  That cannot be good!  So I did pull this out of the foreach loop but that did not really change anything.  The cart.txt file is still getting completely almost completely wiped */
                fwrite($fp,$cart_string); 
                fclose($fp); 
        } 
}

Posted: Tue Oct 24, 2006 11:25 pm
by dagee
Ok I've been analyzing it more see that there is just so much going wrong with this piece of code that I am just going to start this section over again.

Posted: Wed Oct 25, 2006 6:32 am
by volka
Might be a good idea.
In another thread you asked what advantages a database would provide. Think about concurrent access to the data (there might be two users calling the script at virtually the same time). Of course you can build your own locking mechanism (see flock()). But databases are usually smarter than the average bear ;)
sqlite e.g. provides a "server-less" database interface, i.e. you don't have to install a database server like for MySQL or PostgreSQL. see http://de2.php.net/sqlite