Recently Viewed Items

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Recently Viewed Items

Post by zenabi »

I have posted this question before but it got lost during the database crash.

How can I implement a Recently Viewed Items list similar to the ones on Amazon and eBay?

A very kind person posted a solution before (using sessions and arrays) but I can't remember their name. If this is you then please post your code again and accept my apology for forgetting who you are. :oops:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

it's not me who helped you ;) But anyway, here is simple solution (for recently visited urls):

Code: Select all

// file: index.php
session_start();
if(isset($_SESSION['recent'])) {
  echo "Recently visited links:<br />";
  foreach($_SESSION['recent'] as $url) {
     echo "<a href='$url'>$url</a><br />";
  }
}
//........
// file: reg_visit.php
if(!isset($_SESSION['recent'])) {
    $_SESSION['recent'] = array();
}
$qstr = isset($_SERVER['QUERY_STRING']) ? "?{$_SERVER['QUERY_STRING']}" : "";
array_unshift($_SESSION['recent'],$_SERVER['REQUEST_URI'] . $qstr);
$_SESSION['recent'] = array_slice($_SESSION['recent'], 0, 10); // keep only last 10 pages

// each page which should be displayed in 'recent list' must include following code:
session_start();
include 'reg_visit.php';
//..........
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

Thanks for your example!

It's a little different to the example I was given before, but this should get me started. Thanks again.
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

OK I've managed to come up with a system similar to the example given above, but have now stumbled across a problem.

Say I have 5 recently viewed items:

1. Hat
2. Shirt
3. Socks
4. Pants
5. Shoes

Now if I click on item 4 (pants) I want it to jump to the top of the list and "push" all the other items down. Can this be done easily?

Thanks in advance to anyone who can help me accomplish this.
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

yup, just have a field called 'time_viewed' and update it every time a item is viewed.

when showing the list, just do ORDER BY time_viewed DESC :D
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

Thanks qads but I was hoping of finding a way to do it using sessions and arrays and not through a database.

Can anyone else help?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

[php_man]array_unshift[/php_man] will do the job.
zenabi
Forum Commoner
Posts: 84
Joined: Mon Sep 08, 2003 5:26 am
Location: UK

Post by zenabi »

Thanks patrickG I've got it now! With a combination of array_shift() and unset() I've got this:

Code: Select all

<?php
$recent = array("Hat", "Shirt", "Socks", "Pants", "Shoes");

print_r($recent);

$item = $recent[3];  //Pants

for ($i=0; $i < count($recent); $i++)
{
	if ($recent[$i] == $item)
	{
		unset($recent[$i]);
	}
}

array_unshift($recent, $item);

print_r($recent);
?>
Which outputs exactly what I want:

Code: Select all

Array
(
    &#1111;0] =&gt; Hat
    &#1111;1] =&gt; Shirt
    &#1111;2] =&gt; Socks
    &#1111;3] =&gt; Pants
    &#1111;4] =&gt; Shoes
)

Array
(
    &#1111;0] =&gt; Pants
    &#1111;1] =&gt; Hat
    &#1111;2] =&gt; Shirt
    &#1111;3] =&gt; Socks
    &#1111;4] =&gt; Shoes
)
Cheers! :)
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

:)
Post Reply