Jump to content

Last 5 Products Viewed


Hooch

Recommended Posts

Hey all. I have googled with no luck. I'm curious on how to show the last 5 products viewed. It needs to be dynamic enough that if only 1 product wasviewed, then it shows only 1. Then from there keep rolling the last five. (or less) Each item will have it's own unique id saved in the DB already. I get so far then hit a wall. Thank you for your time.

Link to comment
Share on other sites

I don't know how your system is structured, but I'd store a comma-separated list of the product IDs that the person viewed. Then I would use explode() to separate the list into pieces and loop through them.

Link to comment
Share on other sites

  • 4 weeks later...

I finally had some time to research this.I'm having a hard time adding values to the array.I have 3 pages set up with very similar code.The variable "$thisPage" changes for each page. (1,2,3)

<?PHP  session_start();  //Last 5 properties viewed $thisPage = 1; if(!isset($_SESSION[$session_name])) {	 $session_name = session_id();	 $_SESSION[$session_name] = array(); }  array_push($_SESSION[$session_name],$thisPage);  echo '1: ' . $_SESSION[$session_name][0]; echo '<br>'; echo '2: ' . $_SESSION[$session_name][1]; echo '<br>'; echo '3: ' . $_SESSION[$session_name][2]; echo '<br>'; echo '4: ' . $_SESSION[$session_name][3]; echo '<br>'; echo '5: ' . $_SESSION[$session_name][4]; ?>

If I go to page 1, it will print out...1: 12: 3:4:5: If I go to page 2, it will print out...1: 22: 3:4:5: and if I go to page 3...1: 32: 3:4:5: Any help would be great thank you.

Link to comment
Share on other sites

Maybe something where everytime someone views something, you save it to the session variable for that person, which will continue to grow as they view things within that session. Use a loop to only display the last 5 elements of that variable, (so that you are doing LIFO (last in/first out)). To handle it so that it can display less than 5 if they haven't viewed that many yet, set a conditional (within the loop) that checks each iteration to see if there is data, and if there isn't, display "" or something.

Link to comment
Share on other sites

Hi scientist.I was going for that solution in the code you see above.A new session is created when a visitor clicks on one of the pages.Then it is suppose to add everytime a page is viewed.But it's not adding on to the array.

Link to comment
Share on other sites

I see no problem.If the session is not set yet, we need to set it.Then once it's set, we do not need to set it again. :)I just tried echoing the following

 echo $_SESSION[$session_name] . '<br />'; echo $session_name . '<br />';

The actual session echoed nothing.

Link to comment
Share on other sites

I just had a break through.

<?PHP  session_start();  //Last 5 properties viewed $thisPage = 1;  if(!isset($_SESSION[session_id()])) {	 //$session_name = session_id();	 $_SESSION[session_id()] = array(); }  array_push($_SESSION[session_id()],$thisPage);  echo '1: ' . $_SESSION[session_id()][0]; echo '<br>'; echo '2: ' . $_SESSION[session_id()][1]; echo '<br>'; echo '3: ' . $_SESSION[session_id()][2]; echo '<br>'; echo '4: ' . $_SESSION[session_id()][3]; echo '<br>'; echo '5: ' . $_SESSION[session_id()][4]; ?>

It's adding the values to the array:)

Link to comment
Share on other sites

You could have also followed jsg's advice...

Look at the order you have these lines in:if(!isset($_SESSION[$session_name])){ $session_name = session_id();
and moved this line...$session_name = session_id();...outside the if statement, like so:$session_name = session_id();if(!isset($_SESSION[$session_name])){ $_SESSION[$session_name] = array();}That would have saved the trouble of renaming all your variables :)
Link to comment
Share on other sites

Thanks jkloth, I wasn't following JSG. I now have a header issue.Here's my code..

 <?PHP session_start(); //Last 5 properties viewed $thisPage = 3;  $session_name = session_id(); if(!isset($_SESSION[$session_name])) { 	$_SESSION[$session_name] = array(); } echo '<a href="test1.php">1</a>   <a href="test2.php">2</a>   <a href="test3.php">3</a>   <a href="test4.php">4</a>'; echo '<br />';  array_push($_SESSION[$session_name],$thisPage);  echo '1: ' . $_SESSION[$session_name][0]; echo '<br>'; echo '2: ' . $_SESSION[$session_name][1]; echo '<br>'; echo '3: ' . $_SESSION[$session_name][2]; echo '<br>'; echo '4: ' . $_SESSION[$session_name][3]; echo '<br>'; echo '5: ' . $_SESSION[$session_name][4]; echo '<br />print_r:<br />'; print_r ($_SESSION[$session_name]); echo '<br />'; $count = count($_SESSION[$session_name]); echo $count;  ?>

This produces...Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /test3.php:1) in /home/public_html/test3.php on line 3 :) :)

Link to comment
Share on other sites

I didn't really give any advice, just pointed out where the problem was. I wanted you to see that you were trying to use $session_name before you defined it. It looks like you got that worked out. For the sake of efficiency, it will be more efficient if you do define the variable once and use the variable instead of using the session_id function each time. It just takes a little overhead to run the function, so might as well run it once and save the value instead of running it every time you need the value.The session start error means that you have output before you tried to send a header. There's a description about how to read that error message and find the source of the problem in this thread:http://w3schools.invisionzone.com/index.php?showtopic=12509

Link to comment
Share on other sites

Here is the total code...what would be considered a header?

 <?PHP  session_start(); $thisPage = 3;  $session_name = session_id(); if(!isset($_SESSION[$session_name])) { 	$_SESSION[$session_name] = array(); } echo '<a href="test1.php">1</a>   <a href="test2.php">2</a>   <a href="test3.php">3</a>   <a href="test4.php">4</a>'; echo '<br />';  array_push($_SESSION[$session_name],$thisPage); $count = count($_SESSION[$session_name]); $last5 = $count - 5; $output = array_slice($_SESSION[$session_name], $last5);  for ($i = 4; $i >= 0; $i--)  {	 echo $output[$i];	 echo '<br />'; } ?>

oh..btw I have a new error popping upWarning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/connie/public_html/test3.php:1) in /home/connie/public_html/test3.php on line 3Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/connie/public_html/test3.php:1) in /home/connie/public_html/test3.php on line 3

Link to comment
Share on other sites

Headers include cookies, which you can set with either setcookie or session_start (which sends a session cookie), or you can explicitly send any header using the header function. The error occurs when you try to send a header after you've already sent output, because the first time you send any output PHP will send all of the headers first. So, if you try to send a header after you've already sent output you'll get the warning you're seeing.Check the link to the other thread, it describes the situation and explains how to determine from the error message exactly what the cause is. Your error messages are indicating that output was sent on line 1. That most likely means that you have a space or newline character before your opening PHP tag.

Link to comment
Share on other sites

For anyone following here is my final code that will show the last 5 from the array fromthe most recent down.Next I will need to put a check in to stop duplicate entries.Below would be test1.php

<?PHP session_start();$thisPage = 1;//What we want to keep track of$showEntriesNum = 10;//Set to the number of entries to keep track of $session_name = session_id();//Set a unique session name//Only set the session onceif(!isset($_SESSION[$session_name])){	$_SESSION[$session_name] = array();} echo '<a href="test1.php">1</a>   <a href="test2.php">2</a>   <a href="test3.php">3</a>   <a href="test4.php">4</a>';echo '<br />';//Do not echo out duplicatesif( 	($thisPage == $_SESSION[$session_name][9]) || 	($thisPage == $_SESSION[$session_name][8]) || 	($thisPage == $_SESSION[$session_name][7]) || 	($thisPage == $_SESSION[$session_name][6]) || 	($thisPage == $_SESSION[$session_name][5]) || 	($thisPage == $_SESSION[$session_name][4]) || 	($thisPage == $_SESSION[$session_name][3]) || 	($thisPage == $_SESSION[$session_name][2]) || 	($thisPage == $_SESSION[$session_name][1]) || 	($thisPage == $_SESSION[$session_name][0])   ){	//Do Nothing}	else{	array_push($_SESSION[$session_name],$thisPage);//This will append the new entry if it is new with in the last 10 (or whatever $showEntriesNum is)}//If the array count is less than or equal to $showEntriesNum we need to only show what is in the array.$count = count($_SESSION[$session_name]);	if($count <= $showEntriesNum){	for ($i = ($showEntriesNum - 1); $i >= 0; $i--)	{		if(isset($_SESSION[$session_name][$i]))		{			echo $_SESSION[$session_name][$i];			echo '<br />';		}	}	}	else//There is alteast 1 more entry to the array then $showEntriesNum{ 	$showEntries = $count - $showEntriesNum;	$output = array_slice($_SESSION[$session_name], $showEntries);//Show everything past the value of $showEntries 	for ($i = ($showEntriesNum - 1); $i >= 0; $i--)	{		 echo $output[$i];		 echo '<br />';	}}?>

Link to comment
Share on other sites

Hey all...How would I use a "for" loop to shorten this code...plus it could be dynamic too.

if(	($thisPage == $_SESSION[$session_name][9]) ||	($thisPage == $_SESSION[$session_name][8]) ||	($thisPage == $_SESSION[$session_name][7]) ||	($thisPage == $_SESSION[$session_name][6]) ||	($thisPage == $_SESSION[$session_name][5]) ||	($thisPage == $_SESSION[$session_name][4]) ||	($thisPage == $_SESSION[$session_name][3]) ||	($thisPage == $_SESSION[$session_name][2]) ||	($thisPage == $_SESSION[$session_name][1]) ||	($thisPage == $_SESSION[$session_name][0])   )

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...