Here's a little UDT I wrote for a site currently in development which places a 'previous article' / ' next article' link as required on a news details page.
I wrote this so that users didn't have to go back to the parent news summary to navigate between news stories. Something similar could have been achieved by creating a menu like summary list in a column, but this is what my client wanted, so I thought I might share it.
Please note, this navigates ALL news articles, regardless of their category. People are more than welcome to improve on it as they see fit. I'm not much of a programmer, so if anyone can see ways of making it smaller or more efficient, please share with the rest of the community in this topic. Also note, I am using SEF urls for this example. Please alter URL locations as required
Here's the UDT
Code: Select all
global $gCms;
$pageid = ($_REQUEST['pageid']) ? $_REQUEST['pageid'] : $params['pageid'];
$currId = ($_REQUEST['currid']) ? $_REQUEST['currid'] : $params['currid'];
$db = &$gCms->db;
// get all news articles sorted by ascending date
$sql = "SELECT * FROM cms_module_news ORDER BY news_date asc";
$result = mysql_query($sql);
$articlesArray = array();
$articleTitlesArray = array();
while( $row = mysql_fetch_array($result) ) {
array_push($articlesArray,$row['news_id']);
array_push($articleTitlesArray,munge_string_to_url($row['news_title']));
}
$searchValue = $currId;
// search articles array and get current article's index
$arrayLength = count($articlesArray);
$currentIndex = 0;
for($i=0;$i < $arrayLength;$i++) {
if($searchValue == $articlesArray[$i]) {
$currentIndex = $i;
}
}
switch($currentIndex) {
case 0:
// if first news article
$next_uri = 'news/'.$articlesArray[$currentIndex+1].'/'.$pageid.'/'.$articleTitlesArray[$currentIndex+1];
break;
case $arrayLength-1:
// last news article
$prev_uri = 'news/'.$articlesArray[$currentIndex-1].'/'.$pageid.'/'.$articleTitlesArray[$currentIndex-1];
break;
default:
// if an article between the first and the last
$next_uri = 'news/'.$articlesArray[$currentIndex+1].'/'.$pageid.'/'.$articleTitlesArray[$currentIndex+1];
$prev_uri = 'news/'.$articlesArray[$currentIndex-1].'/'.$pageid.'/'.$articleTitlesArray[$currentIndex-1];
break;
}
$smarty->assign('news_next_url', $next_uri);
$smarty->assign('news_prev_url', $prev_uri);
Code: Select all
{article_prev_next pageid=$page_id currid=$entry->id}
<div class="article-nav">
{if $news_prev_url}<div class="article-prev"><a href="{$news_prev_url}">« Previous article</a></div>{/if}
{if $news_next_url}<div class="article-next"><a href="{$news_next_url}">Next article »</a></div>{/if}
</div>
(edit)
Just a note: For this line in the UDT
Code: Select all
$sql = "SELECT * FROM cms_module_news ORDER BY news_date asc";