Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 217009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:37:03+00:00 2026-05-11T18:37:03+00:00

I’m trying to create an array to display the last 5 products a customer

  • 0

I’m trying to create an array to display the last 5 products a customer has viewed.

The array is a 2 dimensional array like below…

$RView= array(
array( ID => “1001”, RefCode => “Ref_01”, Name => “Name_01” ),
…
array( ID => “1005”, RefCode => “Ref_05”, Name => “Name_05” )
);

The array values are retrieved from the products recordset and is designed to function as follows when a customer visits a product page.

  • Page will check if a Session Array exists
  • If yes, an array variable is created from existing Session
    If no, a new array is created.
  • Array will add the new product details.
  • Array will count if there are more than 5 existing products in the array.
  • If yes, it will remove the oldest.
    If no, moves to next step.
  • A Session is created/updated from the revised Array.

My current effort is attached below…
Many thanks for any help.

    <?php 
    session_start() 
    // Get or Create Array
    IF (isset($_SESSION['sessRView'])) {
    $RView = ($_SESSION['sessRView']); } 
    ELSE {
    $RView = array(array()); 
    }

    // Append currently viewed Product to Array
    array(array_unshift($RView, $row_rsPrd['PrdID'], $row_rsPrd['RefCode'], $row_rsPrd['Name']));

    // Check if more than 5 products exist in Array, if so delete.
    IF (sizeof($RView) > 5) {
    array(array_pop($RView)); }

    // Update Session for next page
    $_SESSION['sessRView'] = $RView;

    // Display Array
    for ($row = 0; $row < 5; $row++)
    {
    echo "<ul>";
        echo "<li><a href='?PrdID=".$RView[$row]["PrdID"]."'>".$RView[$row]["RefCode"]."</a> : ".$RView[$row]["Name"]."</li>";
    echo "</ul>";
    }
    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:37:03+00:00Added an answer on May 11, 2026 at 6:37 pm

    It’s more or less right – just 2 lines need to be changed.

    1. There’s no need for the extra array() around array_unshift and array_pop.
    2. When you use array_unshift you’re pushing an array of items (not the id/codes individually) – I think you mean array_unshift($RView, array($prodid,$name,...))
    3. What if $RView doesn’t have 5 elements? In that case you’re accessing undefined array indices (which may or may not show an error). Change it to a foreach loop: e.g.
      foreach ($Rview as $prod) echo $prod['Name']...

    It should work after you make these changes. You might want to clean up the coding style a bit, though 🙂


    EDIT: Oh, I see, when you’re referencing the array in the for loop it doesn’t know that the array has “ProdID” and “Name” indices. When you make an array you have to define the indexes using the => operator.

    • Add indexes to the array when you array_unshift:
      array_unshift($RView, array("ProdID" => $row_rsProd["ProdID"], "Name"...))

    • If row_rsProd isn’t too big, you can just tack the entire row_rsprod onto $RView.
      so change array_unshift(…) to just $RView[] = $row_rsProd
      This way the indexes are preserved.

    • Alternatively you can change the indicies in the for loop to match. Right now the array you unshift onto $RView is 0-based – $RView[0][0] is the product ID for the first product, etc.
      So you can change the stuff in the foreach loop to
      echo "<li>..." $prod[0] $prod[1] $prod[2]

    Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.