I must be tired

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

I must be tired

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

OK, I changed the scope issue

Post 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 />";
		
}
ZephyrWest
Forum Newbie
Posts: 7
Joined: Mon Oct 16, 2006 9:39 pm

Post by ZephyrWest »

You commented out the part where it retrieves the values for $current_cart...
dagee
Forum Commoner
Posts: 33
Joined: Thu Oct 19, 2006 8:31 pm

Wow, I really was tired

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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);
Post Reply