0

I need to save last 10 viewed products in $_SESSION, so I try to use code below, but in this case $_SESSION['lastViewedProductsList'] keeps only last $product

$title = $node->title;
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
$img = $node->uc_product_image['und'][0]['filename'];
$product = array('title' => $title, 'url' => $url, 'img' => $img);

if (!isset($_SESSION['lastViewedProductsList'])) {
  $_SESSION['lastViewedProductsList'] = $product;
} else {
  $_SESSION['lastViewedProductsList'] = $product;
}

How to save last 10 products?

1
  • 2
    Add to $_SESSION['lastViewedProductsList'][] so you can access $_SESSION['lastViewedProductsList'][0], $_SESSION['lastViewedProductsList'][1],...$_SESSION['lastViewedProductsList'][9] Commented Jun 2, 2016 at 13:07

3 Answers 3

1
$title = $node->title;
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
$img = $node->uc_product_image['und'][0]['filename'];
$product = array('title' => $title, 'url' => $url, 'img' => $img);

if (!isset($_SESSION['lastViewedProductsList'])) {
    // If the SESSION parameter does not exists, we create it as an array
    $products = array($product);
} else {
    // Else, we add the product in it
    $products = $_SESSION['lastViewedProductsList'];
    $products[] = $product;
    // We check if the array has more than 10 rows
    if(count($products) > 10){
        // If that's the case, we remove the first line in it to keep 10 rows in it
        array_shift($products);
    }
}

$_SESSION['lastViewedProductsList'] = $products;

If you want to check if the product is already in the array, check this solution: PHP: Check if value and key exist in multidimensional array

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

4 Comments

I guess line $_SESSION['lastViewedProductsList'] = $products; can be placed after if...else (I see it two times here). Or not?
After some thinking I actually don't understand why $_SESSION['lastViewedProductsList'] = $products; placed after if...else works, because array $products is initialized inside if...else and should be undefinied outside, shouldn't it?
Hi Heidel, in any case $products will be defined, in the if or in the else statement. No need to declare it outside.
I see! Thanks a lot for help!
1

You can convert your array in json format with json_encode() function and save it in your session. Or you can even use php serialize, but i personally prefer json becouse object serialization may lead to some vulnerabilities

Comments

1

Try This:

$title = $node->title;
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
$img = $node->uc_product_image['und'][0]['filename'];
$product = array('title' => $title, 'url' => $url, 'img' => $img);

if (!isset($_SESSION['lastViewedProductsList'])) {
$_SESSION['lastViewedProductsList'][] = $product;
} else {
$_SESSION['lastViewedProductsList'][] = $product;
}

3 Comments

How can I add check here to save in $_SESSION['lastViewedProductsList'] only unique products and not save the same product a few times?
if (in_array(product['title'],$_SESSION['lastViewedProductsList'][])) { }

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.