Previous and next article in news details page

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
geeves
Forum Members
Forum Members
Posts: 114
Joined: Wed Dec 03, 2008 4:56 am

Previous and next article in news details page

Post 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 :)
Last edited by geeves on Thu May 07, 2009 1:02 am, edited 1 time in total.
Ara Garabedian
Multimedia Designer / Developer
http://ara.ifky.com.au
JeremyBASS

Re: Previous and next article in news details page

Post by JeremyBASS »

That’s a nice detail for presentation... Look forward to trying it...

Cheers
Jeremy
kuzmany
Power Poster
Power Poster
Posts: 448
Joined: Tue Oct 10, 2006 5:00 pm
Location: Bratislava

Re: Previous and next article in news details page

Post 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>
Yes, it's me: MleCMS, MleDomains, ModuleGenerator, URLWatchdog, Youtuber, Extended Content Blocks, Extended Tools, Analytics etc.
geeves
Forum Members
Forum Members
Posts: 114
Joined: Wed Dec 03, 2008 4:56 am

Re: Previous and next article in news details page

Post by geeves »

That's great.

Thanks veduci. Much appreciated.
Ara Garabedian
Multimedia Designer / Developer
http://ara.ifky.com.au
User avatar
Owens
Forum Members
Forum Members
Posts: 97
Joined: Thu Dec 27, 2007 11:29 pm

Re: Previous and next article in news details page

Post 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=find in Veduci's code]// 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";[/code]

[code=change it to]// 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 status = 'published' ORDER BY news_date DESC  LIMIT 1";[/code]

That's is.
Last edited by Anonymous on Thu Oct 08, 2009 10:36 pm, edited 1 time in total.
User avatar
NikNak
Forum Members
Forum Members
Posts: 183
Joined: Fri Oct 02, 2009 2:28 pm

Re: Previous and next article in news details page

Post 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
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Previous and next article in news details page

Post 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 status = 'published' ORDER BY news_date DESC LIMIT 1";


and replace with:

// get all published and unexpired news articles sorted by ascending date
$query_next = "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 < ? AND (end_time >= CURRENT_DATE OR end_time IS NULL) AND status = 'published' ORDER BY news_date DESC LIMIT 1";
JeremyBASS

Re: Previous and next article in news details page

Post 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
Elise
Forum Members
Forum Members
Posts: 24
Joined: Sun Jan 03, 2010 10:13 pm

Re: Previous and next article in news details page

Post 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?
JeremyBASS

Re: Previous and next article in news details page

Post by JeremyBASS »

did you try my plugin? that what it does. :D
Elise
Forum Members
Forum Members
Posts: 24
Joined: Sun Jan 03, 2010 10:13 pm

Re: Previous and next article in news details page

Post by Elise »

JeremyBASS wrote:did you try my plugin? that what it does. :D


Yes, but it does not work for CGBlog
jmcgin51
Power Poster
Power Poster
Posts: 1899
Joined: Mon Jun 12, 2006 9:02 pm

Re: Previous and next article in news details page

Post 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.
User avatar
Gregor
Power Poster
Power Poster
Posts: 1872
Joined: Thu Mar 23, 2006 9:25 am
Location: The Netherlands

Re: Previous and next article in news details page

Post by Gregor »

Great UDT. Thnkx.

I got one question, at the end of the article it shows:
<< --Add Me - module:News string:next-- |  --Add Me - module:News string:prev-- >>



I assume this was not the idea of the developer ;) any idea how I can make it show just 'Next' | 'Prev'?

Gregor
zzmarker
Forum Members
Forum Members
Posts: 13
Joined: Fri Oct 03, 2008 7:37 am

Re: Previous and next article in news details page

Post 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';
folco3
Forum Members
Forum Members
Posts: 22
Joined: Wed Jun 03, 2009 4:28 am

Re: Previous and next article in news details page

Post by folco3 »

This is cool can it be modified into CGBlog?
Post Reply

Return to “Tips and Tricks”