[ic] How to code most recently viewed items

Marty Tennison interchange-users@interchange.redhat.com
Mon Apr 8 10:42:01 2002


Andrew Brown wrote on Sunday, April 07, 2002 9:22 AM
>
> 	If you have ever been on amazon.com you'll notice it
> shows a history of
> most recent items you have viewed.  I am trying to do this
> same thing where
> it will show the most recent items you have viewed. I guess
> it would work
> when someone clicks an item on the results.html it will add
> that item to the
> most recently viewed.  I need to show a maximum of 4 at a
> time, but they
> keep changing as you click on different items. Can anybody
> point me in the
> right direction on how to set this up. Can it be done?

Hi Andrew.

There are two ways that I can think of to do this and I have
implemented both on my site.  The one that I use now is this...   Put
some code on your flypage that adds the item code to an array.  like
this.  (note: sv_rec_list is the variable name I use, you can use
anything)

[if scratch sv_rec_list !~ /[item-code]/]
  [seti sv_rec_list][item-code]-[scratch sv_rec_list][/seti]
[/if]

Then create some code or module (named recent_pages for example) and
call it from the page you want it to display in.  Or you can just put
the code right in the page if you want it to be there all the time. I
use a module so that if there are no items that they have viewed it
does not display the recent list (empty) at all and bypasses the code.

[include file="templates/components/recent_pages"]


In the module recent_pages you can put some code similar to this. This
is actually working code but  Unfortunately it resemble a bit of
spaghetti due to my part, I have not had time to fix it since it
works.  :)

[perl]
 @sv_list = split(/-/,$Scratch->{sv_rec_list});
 $cnt = 1;
 $Scratch->{sv_rp_more} = @sv_list;
 $rp_limit = 4; # set this to limit of matches desired
 foreach $i (@sv_list) {
	last if $cnt >= $rp_limit;
	my $d = $Tag->data(products, description, $i);
	my $t = $Tag->data(products, description, $i);
	$d = substr($d,0,18); # if space is an issue modify this substring
	$hl .= qq{&nbsp;&#149;<A HREF="[area $i]" title="$t" class="recent"
style="font-size: 9px;">$d</A><BR>};
	++$cnt;
 }
return $hl;
[/perl]

The second way to do this is to use a hacked up version of the
history-scan tag that is put into a loop.   The main problem with this
approach is deciding what URL's are items and what are not.   If all
of your SKU's follow a rigid naming convention such as LL#####  (where
L=letter and #=number) then you can easily write a regular expression
to find the page.  If not, then it is much more difficult to find the
item pages.  I would prefer to use this approach but my SKu's are not
that rigid so the regular expression becomes rather unwieldy to get
things right.  (Hmmm, I just thought of something.  My items all start
with two upper case letters and my pages are all lower case.  As long
as I keep my pages all lower case I can use /\/[A-Z]?.+html/ as a reg
exp and it should work.  Hmmm, I'll have to try that)

Anyway, I hope that helps.   I'll send the code for the hacked up
history-scan tag (sku-scan) if I get it to work.

Marty