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?

Heidel
  • 3,174
  • 16
  • 52
  • 84
  • 2
    Add to `$_SESSION['lastViewedProductsList'][]` so you can access `$_SESSION['lastViewedProductsList'][0], $_SESSION['lastViewedProductsList'][1],...$_SESSION['lastViewedProductsList'][9]` – Thamilhan Jun 02 '16 at 13:07

3 Answers3

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

Riccardo Bonafede
  • 610
  • 1
  • 9
  • 17
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;
}
A. Jain
  • 167
  • 8
  • How can I add check here to save in `$_SESSION['lastViewedProductsList']` only unique products and not save the same product a few times? – Heidel Jun 02 '16 at 13:20
  • if (in_array(product['title'],$_SESSION['lastViewedProductsList'][])) { } – A. Jain Jun 02 '16 at 13:26
  • check it: http://stackoverflow.com/questions/9240251/checking-if-array-value-exists-in-a-php-multidimensional-array – A. Jain Jun 02 '16 at 13:34
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

Community
  • 1
  • 1
Nicolas P.
  • 94
  • 6
  • I guess line `$_SESSION['lastViewedProductsList'] = $products;` can be placed after `if...else` (I see it two times here). Or not? – Heidel Jun 02 '16 at 13:35
  • 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? – Heidel Jun 03 '16 at 07:04
  • Hi Heidel, in any case $products will be defined, in the if or in the else statement. No need to declare it outside. – Nicolas P. Jun 03 '16 at 07:33
  • I see! Thanks a lot for help! – Heidel Jun 03 '16 at 08:56