Page 1 of 2

Previous and next article in news details page

Posted: Wed May 06, 2009 3:46 pm
by geeves
Hi guys.

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);
Here's how I pass data to the UDT via the tag in the news module details template. Next and previous will only display if there is an article chronologically before or after the current news item.

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>
Hope you guys find this useful.

(edit)
Just a note: For this line in the UDT

Code: Select all

$sql = "SELECT * FROM cms_module_news ORDER BY news_date asc";
Rename your table accordingly if you have chosen to use prefixes :)

Re: Previous and next article in news details page

Posted: Wed May 06, 2009 7:56 pm
by JeremyBASS
That’s a nice detail for presentation... Look forward to trying it...

Cheers
Jeremy

Re: Previous and next article in news details page

Posted: Wed Jul 15, 2009 8:39 pm
by kuzmany
my solution without db query for all news item

prev_next UDT

Code: Select all

if (!function_exists('MyGetModuleInstance'))
{
        function &MyGetModuleInstance($module)
        {
                global $gCms;

                if (isset($gCms->modules[$module]) &&
                        $gCms->modules[$module]['installed'] == true &&
                        $gCms->modules[$module]['active'] == true)
                {
                        return $gCms->modules[$module]['object'];
                }
                // Fix only variable references should be returned by reference
                $tmp = FALSE;
                return $tmp;
        }
}

global $gCms;
$db = &$gCms->db;

$news = MyGetModuleInstance('News');


$pageid = ($_REQUEST['pageid']) ? $_REQUEST['pageid'] : $params['pageid'];
$currdate = $params['currdate'];
if(!$currdate) return;

// get all news articles sorted by ascending date
$query_next = "SELECT news_id,news_title FROM  ".cms_db_prefix()."module_news WHERE news_date > ?  ORDER BY news_date ASC LIMIT 1";
$query_prev = "SELECT news_id,news_title FROM ".cms_db_prefix()."module_news WHERE news_date < ?  ORDER BY news_date DESC  LIMIT 1";

$result_next = &$db->GetRow($query_next,array($currdate));
$result_prev = &$db->GetRow($query_prev,array($currdate));

if($result_next['news_id']) {
$aliased_title = munge_string_to_url($result_next['news_title']);
$prettyurl = 'news/' . $result_next["news_id"] .'/'.$pageid."/$aliased_title";
$next_uri = $news->CreateLink('cntnt01', 'detail', $pageid, '', array('articleid' => $result_next["news_id"]) ,'', true, false, '', true, $prettyurl);
}else{
$next_uri = "";
}


if($result_prev['news_id']) {
$aliased_title = munge_string_to_url($result_prev['news_title']);
$prettyurl = 'news/' . $result_prev["news_id"] .'/'.$pageid."/$aliased_title";
$prev_uri = $news->CreateLink('cntnt02', 'detail', $pageid, '', array('articleid' => $result_prev["news_id"]) ,'', true, false, '', true, $prettyurl);
}else{
$prev_uri = "";
}

$smarty->assign('news_next_url', $next_uri);
$smarty->assign('news_next_text', $news->lang("next"));
$smarty->assign('news_prev_url', $prev_uri);
$smarty->assign('news_prev_text', $news->lang("prev"));
smarty in detail template

Code: Select all

{prev_next pageid=$page_id currid=$entry->id currdate=$entry->postdate}
<p>
{if $news_next_url}<a href="{$news_next_url}"><< {$news_next_text}</a> | {/if}
{if $news_prev_url}<a href="{$news_prev_url}">{$news_prev_text} >></a>{/if}
<p>

Re: Previous and next article in news details page

Posted: Wed Jul 15, 2009 10:43 pm
by geeves
That's great.

Thanks veduci. Much appreciated.

Re: Previous and next article in news details page

Posted: Thu Oct 08, 2009 10:32 pm
by Owens
Veduci's code is good. But it would display links to un-published articles (aka draft status). So I made one small change to skip articles that are un-published, and go to the next published article.

In the WHERE clause, of the MySQL query, I simply added AND status = 'published'

Here is how to do it...

Code: Select all

// get all news articles sorted by ascending date
$query_next = "SELECT news_id,news_title FROM  ".cms_db_prefix()."module_news WHERE news_date > ?  ORDER BY news_date ASC LIMIT 1";
$query_prev = "SELECT news_id,news_title FROM ".cms_db_prefix()."module_news WHERE news_date  ? AND status = 'published' ORDER BY news_date ASC LIMIT 1";
$query_prev = "SELECT news_id,news_title FROM ".cms_db_prefix()."module_news WHERE news_date < ? AND status = 'published' ORDER BY news_date DESC  LIMIT 1";
That's is.

Re: Previous and next article in news details page

Posted: Mon Oct 26, 2009 5:27 pm
by NikNak
Hi guys

Is there an extra tweak to make this only show the next or previous entries from the current article's category.

I can't find a way to view the correct smarty variables in this case.

Much obliged

Nik

Re: Previous and next article in news details page

Posted: Sat Dec 12, 2009 3:39 am
by jmcgin51
thanks to all who contributed to this helpful UDT!!

Here is an additional tweak I added to make sure that expired articles are not displayed via the previous/next links:

Find
// get all news articles sorted by ascending date
$query_next = "SELECT news_id,news_title FROM ".cms_db_prefix()."module_news WHERE news_date > ? AND status = 'published' ORDER BY news_date ASC LIMIT 1";
$query_prev = "SELECT news_id,news_title FROM ".cms_db_prefix()."module_news WHERE news_date ? AND (end_time >= CURRENT_DATE OR end_time IS NULL) AND status = 'published' ORDER BY news_date ASC LIMIT 1";
$query_prev = "SELECT news_id,news_title FROM ".cms_db_prefix()."module_news WHERE news_date = CURRENT_DATE OR end_time IS NULL) AND status = 'published' ORDER BY news_date DESC LIMIT 1";

Re: Previous and next article in news details page

Posted: Mon Jan 18, 2010 12:36 am
by JeremyBASS
FWIW I have something like this in my slack mod... It does this and sorts by cats if you wish... gives titles for title attributes…  check it out if you like...

Cheers
JeremyBass

Re: Previous and next article in news details page

Posted: Mon Mar 08, 2010 11:15 am
by Elise
I'm using the UDT and it is great, thank you!

Just 1 problem, I have 2 categories and I would like to skip the items of one category.

I tried:

$query_next = "SELECT cgblog_id,cgblog_title FROM  ".cms_db_prefix()."module_cgblog WHERE cgblog_date > ? AND category.name = 'Dutch' AND status = 'published' ORDER BY cgblog_date ASC LIMIT 1";

Does anyone know why this does not work?

Re: Previous and next article in news details page

Posted: Mon Mar 08, 2010 2:50 pm
by JeremyBASS
did you try my plugin? that what it does. :D

Re: Previous and next article in news details page

Posted: Mon Mar 08, 2010 6:08 pm
by Elise
JeremyBASS wrote: did you try my plugin? that what it does. :D
Yes, but it does not work for CGBlog

Re: Previous and next article in news details page

Posted: Mon Mar 08, 2010 6:45 pm
by jmcgin51
this thread is about the News module.  Please post a new topic in the Modules/Add-ons forum, since your question is related to jeremybass's SlackMod.

Re: Previous and next article in news details page

Posted: Tue Mar 30, 2010 5:57 am
by Gregor
Great UDT. Thnkx.

I got one question, at the end of the article it shows:
>
I assume this was not the idea of the developer ;) any idea how I can make it show just 'Next' | 'Prev'?

Gregor

Re: Previous and next article in news details page

Posted: Mon Jun 21, 2010 11:41 am
by zzmarker
I had the same problem.

Add to your (dutch?) News module lang-file two new lines like:
$lang['next'] = 'xxxxxxx';
$lang['prev'] = 'xxxxxx';

Re: Previous and next article in news details page

Posted: Fri Oct 29, 2010 3:46 am
by folco3
This is cool can it be modified into CGBlog?