Recently Viewed Content

Last modified: July 28, 2009 - 14:13

Code from this thread to display Recently Viewed Content (much like a breadcrumb trail).

<?php
// the number of items to return, alter to suit.
$limit = 10;

//Only show pages whose paths match this expression. Use SQL 'like' syntax. Change to suit.
$url_filter = '';

global
$user;
$criteria = array();
$where = '';

if (
$user->uid == 'Anonymous' ) {
 
// This is an anonymous user, so track using the hostname
 
$criteria[] = sprintf("(uid = 'Anonymous' AND hostname = '%s')", $user->hostname);
} else {
 
// Or if this user isn't Anonymous, then use the uid (which is more reliable)
 
$criteria[] = sprintf("uid = '%s'", $user->uid);
}
if (!empty(
$url_filter)) {
 
$criteria[] = sprintf(" url LIKE '%s' ", $url_filter);
}
if (!empty(
$criteria)) {
 
$where = ' WHERE ' . implode(' AND ', $criteria);
}

$recently_viewed_query = "SELECT DISTINCT title, path FROM `accesslog` $where ORDER BY timestamp DESC LIMIT 0, $limit";
$result = db_query($recently_viewed_query);

// Now walk through the results and display them
while ($row = db_fetch_object($result)) {
 
$links[] =  l($row->title ? $row->title : t('Home'), $row->path);
}

print
theme('item_list', $links);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.