Play values in an array

Asked

Viewed 81 times

1

I’m trying to play some values in a session variable to later use it in a shopping cart, but I’m not getting it, the values are the id and the quantidade. What I did was this:

The form:

<form method="get" action="addtocart.php">
<div class="product-quantity">
	<span>Quantity:</span> 
		<input type="hidden" name="id" value="<?php echo $prodr['id']; ?>">
		<input type="text" name="quant" value="1">
</div>
<div class="shop-btn-wrap">
	<input type="submit" class="button btn-small" value="Add to Cart">
</div>
</form>

session_start();

if(isset($_GET) && !empty($_GET)){

    $id = $_GET['id'];
    $quant = $_GET['quant'];

    if (isset($quant) && !empty($quant))    { 
          $quant = $_GET['quant']; 
        } else { 
          $quant = 1;
    }

    $_SESSION['cart'][$id] = array("quantity" => $quant);   
    print_r($_SESSION['cart'][$id]);
    exit;

    header('location: cart.php');

} else {
    header('location: cart.php');
}

The variables $id and $quant have values, but when having display by print_r() nothing is displayed.

As suggested, follows test results:

print_r($_GET);
Array ( [id] => 21 [quant] => 1 )
print_r($_SESSION);
Array ( [cart] => 1 [id] => Array ( [19] => 19 ) [quanr] => Array ( [19] => 1 ) [quant] => Array ( [19] => 1 ) )

That second print_r appeared this [quanr] and I don’t know where he came from.

  • Are you sure you are sending via GET?

  • Yes, this is being sent by GET and have values.

  • Strange. I tested here with a simple form and it worked normal.

  • print_r($_GET); and print_r($_SESSION); after the first line give what ?

  • What exactly happens? If you enter the condition, you should display the result of print_r, otherwise it will be redirected. Which of the two occurs? Taking advantage of the contact, why use & instead of && in condition?

  • 1

    Gives a session_destroy() and starts the tests again. It seems that you are accumulating a lot rubbish in the previous typing errors session.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.