I want to add multiple values to array each time when i pass some values to this page(cart.php).
but now i am able to get only one value from array.i also tried
echo $_SESSION['cart'][1];
echo $_SESSION['cart'][2];
but i am able to get only one value which is newly added and i am getting notice
Notice: Undefined offset: 1
Notice: Undefined offset: 2
cart.php
<?php
if(isset($_GET['action']) && isset($_GET['product_id'])){
if($_GET['action'] == "add"){
$product_id = $_GET['product_id'];
$_SESSION['cart'] = array();
array_push($_SESSION['cart'],$product_id);
}
}
?>
this is how i am passing the values in query string
http://localhost/mycart.php?action=add&product_id=4
You always overwrite the array and so, you get only one value in it. Try this:
if(isset($_GET['action']) && isset($_GET['product_id'])){
if($_GET['action'] == "add"){
$product_id = filter_var($_GET['product_id'], FILTER_SANITIZE_NUMBER_INT);
$_SESSION['cart'][] =$product_id;
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加