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!
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.
$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?
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?
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.
<?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...