Page 1 of 1

I must be tired

Posted: Thu Nov 02, 2006 8:51 pm
by dagee
I must really tired of this program I am writing. Currently I am trying to test some logic statements. Each time I end up having a problem I go back and make the program more and more basic. So now I am down to what should be a really simple program to write. And yet it doesn't work correctly What am I missing? Please - it must be something really obvious and I must be just exhausted because this is just not working.

Code: Select all

$name = trim($_POST['name']);
$date = trim($_POST['date']);
$cart = trim($_POST['cart']);
$reservation = trim($_POST['reservation']);// this can have two values either reserve or cancel

//take the data from cartCheckoutInput.html page and create an arrary called $cart_array
$cart_array = array(
  'name' => $name,
  'date' => $date,
  'cart' => $cart
);

//FUNCTIONS

//***************Cancel Function:********************** 

	// read the carts.txt file as an array

	// create a new array that don't have: name.date.cart that user input

	// replace the old carts.txt file with the new array

function cancel()
{
	$file=file("carts.txt"); //grab carts.txt and store as an array. This array is serialized
	foreach ($file as $line)
	{
		$my_line = trim($line);
		$current_cart = unserialize($my_line);
		echo $current_cart['date'];//prints fine
		echo "<br />";
		echo $cart_array['date'];//does not print
		echo "<br />";
		echo $current_cart['cart'];//prints fine
		echo "<br />";
		echo $cart_array['cart'];//does not print
		echo "<br />";
function is called a little later. Currently $current_cart['date'] and $current_cart['cart'] both print to the screen but $cart_array['date']; and
$cart_array['cart']; do not. Shouldn't they. If I want to print an element from an array aren't I right in how to do this? Am I going crazy?

Posted: Thu Nov 02, 2006 9:10 pm
by feyd
[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.
$cart_array is declared and defined outside of the function therefore doesn't exist inside the function. It's a scope issue.

http://php.net/language.variables.scope

OK, I changed the scope issue

Posted: Thu Nov 02, 2006 9:32 pm
by dagee
Ok, I did not know about that scope issue. Thank so much for pointing that out. So I created the variables within the scope of the function and now nothing is printing to the screen. Is this another lask of understanding error on my part or just missing something really obvious?

Code: Select all

function cancel()
{
$name = trim($_POST['name']);
$date = trim($_POST['date']);
$cart = trim($_POST['cart']);
$reservation = trim($_POST['reservation']);// this can have two values either reserve or cancel

//take the data from cartCheckoutInput.html page and create an arrary called $cart_array
$cart_array = array(
  'name' => $name,
  'date' => $date,
  'cart' => $cart
);
	/*$file=file("carts.txt"); //grab carts.txt and store as an array. This array is serialized
	foreach ($file as $line)
	{
		$my_line = trim($line);
		$current_cart = unserialize($my_line);
		
		
		 //if (strtotime($current_cart['date'])==strtotime($cart_array['date']) && $current_cart['cart']==$cart_array['cart'])
		 //{
		//	echo "same";
		// }
	}
	*/ 
	echo $current_cart['date'];
	echo "<br />";
	echo $cart_array['date'];
	echo "<br />";
	echo $current_cart['cart'];
	echo "<br />";
	echo $cart_array['cart'];
	echo "<br />";
		
}

Posted: Thu Nov 02, 2006 10:15 pm
by ZephyrWest
You commented out the part where it retrieves the values for $current_cart...

Wow, I really was tired

Posted: Fri Nov 03, 2006 12:43 pm
by dagee
I must have looked at that same section of code a hundred times and never noticed that. Thank you so much. I knew it was something really obvious. I think I need some time off from this program - it is making me stupider by the second.

Dag

Posted: Fri Nov 03, 2006 2:19 pm
by RobertGonzalez
From your original (try this)...

Code: Select all

<?php
$name = trim($_POST['name']);
$date = trim($_POST['date']);
$cart = trim($_POST['cart']);
$reservation = trim($_POST['reservation']);// this can have two values either reserve or cancel

//take the data from cartCheckoutInput.html page and create an arrary called $cart_array
$cart_array = array(
  'name' => $name,
  'date' => $date,
  'cart' => $cart
);

//FUNCTIONS

//***************Cancel Function:**********************

        // read the carts.txt file as an array

        // create a new array that don't have: name.date.cart that user input

        // replace the old carts.txt file with the new array

function cancel($cart)
{
        if (!is_array($cart))
        {
            die('this is enforcing a passed array');
        }

        $cart_array = cart;
        $file=file("carts.txt"); //grab carts.txt and store as an array. This array is serialized
        foreach ($file as $line)
        {
                $my_line = trim($line);
                $current_cart = unserialize($my_line);
                echo $current_cart['date'];//prints fine
                echo "<br />";
                echo $cart_array['date'];//does not print
                echo "<br />";
                echo $current_cart['cart'];//prints fine
                echo "<br />";
                echo $cart_array['cart'];//does not print
                echo "<br />";
Then call the function (after filling the $cart_array array) like this...

Code: Select all

// Assumed $cart_array is an array
cancel($cart_array);