0

The products in my cart are stored by ajax and set into sessions. look my code from cart_functions.php (here are functions for my buttons in cart).

if (isset($_GET['action'])) {
  $action = $_GET['action'];   //get button action name
  $prod   = $_GET['prod_id'];  // id of the product
  $prodname = 'product_'.$prod;// name of the product



 switch ($action) {
   case 'add':
       $result = add_prod($prod, $prodname);
   break;
  ///rest of switch....(not important for now)

This is the function that adds a product into session

function add_prod($prod, $prodname){
  //add function
$_SESSION[$prodname] = 1;
return ['result'=>'success'];
}

the name of this session is like this--> $_SESSION['product_123'] etc.

Ok so now i want to unset all the $_SESSION[$prodname]. In other pages $_SESSION[$prodname] is $_SESSION['product_123']. So as in my cart i have multiple $_SESSION[$prodname] i want to unset when the client submit order. How i can get all $_SESSION['product_123'],$_SESSION['product_1234']...and so on?

I dont know if this helps but to count my products i used a code from a help i had here... look

$product_count = count(array_filter(array_keys($_SESSION), function($x) {
            return substr($x, 0, 8) == 'product_';
            }));

3 Answers 3

1

You'd better use an array

$_SESSION['products'][$id]

It allow you to easily loop through your products and you can reset it simply by

unset($_SESSION['products'])

Sign up to request clarification or add additional context in comments.

1 Comment

creating session as $_SESSION['products'][$id] is way much better,i had to rewrite my whole code to make $_SESSION['name here'][a id here]
1

At the point at which you are hoping to unset the particular session element, have you tried

unset($_SESSION[$product]);

as that should be all that is required. I searched your code but could not find where you were attemting to unset anything.

4 Comments

got it - tell me, is the new order being inserted successfully?
hmm, i see the unset($_SESSION['Products_cart']); but i don't see in any of this where you are setting $_SESSION['Products_cart']; is there more code where that is set?
look i updated my code with $_SESSION['Products_cart']
i think i'm losing track of the issue here. if the issue is "how do i unset the session element" then the answer must be unset(_SESSION[element]). but if the question is about the entire broad base of code, i'm not sure we can solve it in this format. can you clarify the question and reduce the amount of code necessary to answer, please?
0

try session_destroy() create a new file something like reset.php and you put session_destroy() this erase all session definitely

1 Comment

I want to unset only my products_$id, not all sessions mate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.