Create Recently Viewed list, PHP Array
How to show a list of recently viewed products in PHP
Mostly when you are visiting ecommerce websites, you will see a short browsing history of recently view products. This list allows the user to visit back again more easily to a product page they have just seen or add it to a wishlist or as a reminder. So that they don’t forget about something which may have interested them.
In this tutorial, you will learn how to display the last 5 products viewed, by storing the product ID from the product details page in an array within the session.
Steps:
Here are some steps that we need to follow to achieve…
• Add the clicked product's ID to an array when the product detail page is visited by a user.
• Store up to 5 product IDs in the array.
• Skip the same product being added to the array twice.
• Show the collected product IDs in an array.
Step 1: Adding the product ID to an array.
At the top of the product detail page, create a session, and set the value.
Here, we used 'recentlyviewed' as the session array name.
Step 2: Using the URL Parameter.
Now use the URL Parameter, GET[‘ProductID’], and assign it to $currentitem, This is added to a session array.
Next, create a PHP variable called $maxelements, and set the value to 5.
(Set this to any number, according to how many IDs you wish to store in the array.)
Check that product details page has a URL parameter set, or an empty string.
Check the number of elements in an array.
If there are 5 elements in my array already, I then use the PHP function array_slice() to remove the first array element.
Now, use the PHP function array_push() to add the current itemid to the array.
Step 3: Removing duplicates.
Make sure that the same product isn’t added to the array twice.
If the productid is already there in the array, before pushing the array element onto the end of the array, remove it.
This is done with the PHP function array_diff().
Step 4: Display the array values.
Here is the code example
demo.php
<?php
@session_start();
if(!isset($_SESSION["recentlyviewed"])) {
$_SESSION["recentlyviewed"] = array();
}
$maxelements = 5;
//$currentitem = $_GET['Productid']
$currentitem = rand(0,10); echo $currentitem."<br />"; // check for url parameter
if (isset($currentitem) && $currentitem <> "") { // if product id is already in the array
if (in_array($currentitem, $_SESSION["recentlyviewed"])) { $_SESSION["recentlyviewed"] = array_diff($_SESSION["recentlyviewed"],array($currentitem));//remove it $_SESSION["recentlyviewed"] = array_values($_SESSION["recentlyviewed"]); //re-index the array } $n = count($_SESSION["recentlyviewed"]); if ( $n >= $maxelements) { //check the number of array elements $_SESSION["recentlyviewed"] = array_slice($_SESSION["recentlyviewed"],1); // remove the first element if there are 5 already
//add the current itemid to the array array_push($_SESSION["recentlyviewed"],$currentitem);
}
else {
//add the current itemid to the array
array_push($_SESSION["recentlyviewed"],$currentitem); } } print_r($_SESSION["recentlyviewed"]);
?>
Conclusion:
In this tutorial, you learned 'How to create recently viewed items list in PHP Session array'. Please share your comments to make it better. Thank you for visiting.
Comments
Post a Comment