CMS Made Simple Forums
https://forum.cmsmadesimple.org/

Previous and next article in news details page
https://forum.cmsmadesimple.org/viewtopic.php?f=4&t=33554
Page 1 of 2

Author:  geeves [ Wed May 06, 2009 3:46 pm ]
Post subject:  Previous and next article in news details page

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
\$1:
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.
\$1:
{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
\$1:
$sql = "SELECT * FROM cms_module_news ORDER BY news_date asc";

Rename your table accordingly if you have chosen to use prefixes :)

Author:  JeremyBASS [ Wed May 06, 2009 7:56 pm ]
Post subject:  Re: Previous and next article in news details page

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

Cheers
Jeremy

Author:  kuzmany [ Wed Jul 15, 2009 8:39 pm ]
Post subject:  Re: Previous and next article in news details page

my solution without db query for all news item

prev_next UDT

\$1:
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

\$1:
{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>

Author:  geeves [ Wed Jul 15, 2009 10:43 pm ]
Post subject:  Re: Previous and next article in news details page

That's great.

Thanks veduci. Much appreciated.

Author:  Owens [ Thu Oct 08, 2009 10:32 pm ]
Post subject:  Re: Previous and next article in news details page

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.

Author:  NikNak [ Mon Oct 26, 2009 5:27 pm ]
Post subject:  Re: Previous and next article in news details page

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

Author:  jmcgin51 [ Sat Dec 12, 2009 3:39 am ]
Post subject:  Re: Previous and next article in news details page

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
\$1:
// 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:

\$1:
// 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";

Author:  JeremyBASS [ Mon Jan 18, 2010 12:36 am ]
Post subject:  Re: Previous and next article in news details page

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

Author:  Elise [ Mon Mar 08, 2010 11:15 am ]
Post subject:  Re: Previous and next article in news details page

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?

Author:  JeremyBASS [ Mon Mar 08, 2010 2:50 pm ]
Post subject:  Re: Previous and next article in news details page

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

Author:  Elise [ Mon Mar 08, 2010 6:08 pm ]
Post subject:  Re: Previous and next article in news details page

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


Yes, but it does not work for CGBlog

Author:  jmcgin51 [ Mon Mar 08, 2010 6:45 pm ]
Post subject:  Re: Previous and next article in news details page

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.

Author:  Gregor [ Tue Mar 30, 2010 5:57 am ]
Post subject:  Re: Previous and next article in news details page

Great UDT. Thnkx.

I got one question, at the end of the article it shows:
\$1:
<< --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

Author:  zzmarker [ Mon Jun 21, 2010 11:41 am ]
Post subject:  Re: Previous and next article in news details page

I had the same problem.

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

Author:  folco3 [ Fri Oct 29, 2010 3:46 am ]
Post subject:  Re: Previous and next article in news details page

This is cool can it be modified into CGBlog?

Page 1 of 2 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/