Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP: Session 2-Dimensional Array - Track Viewed Products
    text
    copied!<p>I'm trying to create an array to display the last 5 products a customer has viewed.</p> <p>The array is a 2 dimensional array like below...</p> <p>$RView= array( array( ID => "1001", RefCode => "Ref_01", Name => "Name_01" ), ... array( ID => "1005", RefCode => "Ref_05", Name => "Name_05" ) ); </p> <p>The array values are retrieved from the products recordset and is designed to function as follows when a customer visits a product page.</p> <ul> <li>Page will check if a Session Array exists</li> <li>If yes, an array variable is created from existing Session<br> If no, a new array is created.</li> <li>Array will add the new product details.</li> <li>Array will count if there are more than 5 existing products in the array.</li> <li>If yes, it will remove the oldest.<br> If no, moves to next step. </li> <li>A Session is created/updated from the revised Array. </li> </ul> <p>My current effort is attached below...<br> Many thanks for any help.</p> <pre><code> &lt;?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) &gt; 5) { array(array_pop($RView)); } // Update Session for next page $_SESSION['sessRView'] = $RView; // Display Array for ($row = 0; $row &lt; 5; $row++) { echo "&lt;ul&gt;"; echo "&lt;li&gt;&lt;a href='?PrdID=".$RView[$row]["PrdID"]."'&gt;".$RView[$row]["RefCode"]."&lt;/a&gt; : ".$RView[$row]["Name"]."&lt;/li&gt;"; echo "&lt;/ul&gt;"; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload