Problem with array_push

Hi

I’m struggling with a Session variable, which I’m creating.
It doesn’t seem to persist, when invoked in a function.

Here’s the code:


$_SESSION['cart'] = array();
$firstname = ($_POST['firstname']);
$lastname = ($_POST['lastname']);
$address = ($_POST['address']);

if($_SERVER['REQUEST_METHOD'] == 'POST' && is_numeric($_POST['quantity']) && ($_POST['quantity'] <= 9999)){
$product_id = trim($_POST['product_id']) - 1;
$quantity = trim($_POST['quantity']);
array_push($_SESSION['cart'], array($product[$product_id][0], $product[$product_id][1], $product[$product_id][2], $quantity, $product_id+1));
form_display();
}

The problem appears when I add a product, the product goes to the cart just fine, but when I add another, it owerwrites the first one, so only the first position of the array. Also I’m using sticky forms, but when I use another add_to_cart submit, the sticky data disappears, firstname etc.
But array_push should add the data to the end of the array?

Also, this is the global scope but when I try to use this data inside a function it disappears. SESSION is superglobal, so I don’t need to include it in global;


function order(){
....
for ($i = 0, $counter = count($_SESSION['cart']); $i <= $counter; $i++) {
$query5 = "INSERT INTO order_items (order_id, product_id, quantity) VALUES ('".$order_id."', '".$_SESSION['cart'][$i][4]."', '".$_SESSION['cart'][$i][3]."')";
mysqli_query($dbc, $query5) or die('MySQL Error order_item: '.mysqli_error($dbc).' ('.mysqli_errno($dbc).')');
}
mysqli_close($dbc);
}

The remove code I posted removes only the data from the index,
but it should remove the array position on index.

No it won’t. I just tested it out then too make sure and the following simply returned ‘remove’.

echo '<a href="test.php?cart=remove&amp;index=1">Hi</a> ';

if($_GET['cart']) {
    echo "\
" . $_GET['cart'];
}

If you mean it doesn’t persist over pages, that’s because you’re destroying the value every time you set it to an array.

$_SESSION['cart'] = array();

Should be:

if(!array_key_exists('cart', $_SESSION) || !is_array($_SESSION['cart'])){
    $_SESSION['cart'] = array();
}

wouldnt using & make PHP think that the variables in the get array were $_GET[‘cart’] and $_GET[‘amp;index’], thus causing it to misfire on which index to remove?

That is the case, I now see. Thank you

I have one more problem:


// This sets the links for every product in cart.
echo'<tr> <td>'.$_SESSION['cart'][$i][0].'</td> <td>'.$_SESSION['cart'][$i][1].'&nbsp;&nbsp;EUR</td> <td>'.$_SESSION['cart'][$i][3].'</td> 
<td>'.$sum.'&nbsp;&nbsp;EUR</td> <td><a href="shop.php?cart=remove&amp;index='.$i.'">Remove</a></td> </tr>';

// This should remove the data from cart.
if($_GET['cart']=='remove'){
$key = (int)$_GET['index'];
form_display();
unset($_SESSION['cart'][$key]);
header('Location: shop.php');
}

The header function doesn’t even run.
It seems to work, but it only removes the last elements and when removing from the beginning of the array it doesn’t seem to work.

That doesn’t matter. Each request, you’re resetting the cart to an empty array. Just try my code and see what happens.

Hmm, I have only one script file.
It works on 1 page, and I start the session in the top of the page.

dont use & in your link. Use an actual &.

no, use &
& causes an html warning (entity not defined or somesuch)