ticallian Posted May 1, 2009 Share Posted May 1, 2009 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. 1. Page will check if a Session Array exists. 2a. If yes, an array variable is created from existing Session. 2b. If no, a new array is created. 3. Array will add the new product details. 4. Array will count if there are more than 5 existing products in the array. 5a. If yes, it will remove the oldest. 5b. If no, moves to step 6. 6. 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156353-solved-php-session-2-dimensional-array-track-viewed-products/ Share on other sites More sharing options...
laffin Posted May 1, 2009 Share Posted May 1, 2009 Looks okay, whats the problem? Quote Link to comment https://forums.phpfreaks.com/topic/156353-solved-php-session-2-dimensional-array-track-viewed-products/#findComment-823212 Share on other sites More sharing options...
ticallian Posted May 1, 2009 Author Share Posted May 1, 2009 The problem i'm having is that I can't get any values from the array, the only thing that is being produced is a blank HTML list, that adds a new row each time the page is refreshed (Up to 5 rows). I've revised the code a little since the first post. 1. array(array_unshift()) changed to array_unshift(array()) 2. array(array_pop($RView)) changed to array_pop($RView) 3. for ($row = 0; $row < 5; $row++) changed to foreach ($RView as $Product) See below for the revised version. session_start() // Get or Create Array IF (isset($_SESSION['sessRView'])) { $RView = ($_SESSION['sessRView']); } ELSE { $RView = array(array()); } // Append currently viewed Product to Array array_unshift($RView, array($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_pop($RView); } // Update Session for next page $_SESSION['sessRView'] = $RView; // Display Array foreach ($RView as $prod) { echo "<ul>"; echo "<li><a href='?PrdID=".$prod["PrdID"]."'>".$prod["RefCode"]."</a> : ".$prod["Name"]."</li>"; echo "</ul>"; ;} Anyone with any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/156353-solved-php-session-2-dimensional-array-track-viewed-products/#findComment-823256 Share on other sites More sharing options...
ticallian Posted May 1, 2009 Author Share Posted May 1, 2009 My problem was solved with the following function used instead of my original code. Thanks to "kkeith29" over at daniweb Here's the code if it helps anyone in future. <?php session_start(); function addProductToSession( $id,$code,$name ) { $recView =& $_SESSION['recently_viewed']; if ( count( $recView ) == 5 ) { array_shift( $recView ); } $recView[] = array( $id,$code,$name ); } function getRecentlyViewedProducts() { $ul = "<ul>\n"; foreach( $_SESSION['recently_viewed'] as $data ) { list( $id,$code,$name ) = array_values( $data ); $ul .= "\t<li><a href=\"?PrdID={$id}\">{$code}</a> : {$name}</li>\n"; } $ul .= "</ul>"; echo $ul; } //Query database for product info //The $row_rsPrd is from the database result addProductToSession( $row_rsPrd['PrdID'], $row_rsPrd['RefCode'], $row_rsPrd['Name'] ); getRecentlyViewedProducts(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156353-solved-php-session-2-dimensional-array-track-viewed-products/#findComment-823298 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.