<?php

$us_id = intval($map['us_id']);

$secondary_cat = $GLOBALS['SITE_DB']->query_select_value_if_there('news_category_entries', 'news_entry_category', array('news_entry' => $us_id));
$prev_id = $GLOBALS['SITE_DB']->query_select_value_if_there('news_category_entries', 'news_entry', array('news_entry_category' => $secondary_cat), ' AND news_entry<' . strval($us_id));

if ($prev_id === null) {
    return;
}

$prev_url = build_url(array('page' => 'news', 'type' => 'view', 'id' => $prev_id), '_SEARCH');

echo '<a href="' . escape_html($prev_url->evaluate()) . '">Previous Episode</a>';
