• twitter image
  • facebook image
  • youtube image
  • linkedin image
Language: CMS Made Simple Czech CMS Made Simple France CMS Made Simple Spain CMS Made Simple Hungary CMS Made Simple Russia CMS Made Simple Netherlands

All times are UTC




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Previous and next article in news details page
PostPosted: Wed May 06, 2009 3:46 pm 
Offline
Forum Members
Forum Members

Joined: Wed Dec 03, 2008 4:56 am
Posts: 114
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 :)

_________________
Ara Garabedian
Multimedia Designer / Developer
http://ara.ifky.com.au


Last edited by geeves on Thu May 07, 2009 1:02 am, edited 1 time in total.

Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Wed May 06, 2009 7:56 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Wed Jan 02, 2008 11:39 pm
Posts: 2102
Location: Lewiston-Idaho
That’s a nice detail for presentation... Look forward to trying it...

Cheers
Jeremy


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Wed Jul 15, 2009 8:39 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Tue Oct 10, 2006 5:00 pm
Posts: 448
Location: Bratislava
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>

_________________
Yes, it's me: MleCMS, MleDomains, ModuleGenerator, URLWatchdog, Youtuber, Extended Content Blocks, Extended Tools, Analytics etc.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Wed Jul 15, 2009 10:43 pm 
Offline
Forum Members
Forum Members

Joined: Wed Dec 03, 2008 4:56 am
Posts: 114
That's great.

Thanks veduci. Much appreciated.

_________________
Ara Garabedian
Multimedia Designer / Developer
http://ara.ifky.com.au


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Thu Oct 08, 2009 10:32 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Thu Dec 27, 2007 11:29 pm
Posts: 97
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.

_________________
Studio-Owens.com


Last edited by Anonymous on Thu Oct 08, 2009 10:36 pm, edited 1 time in total.

Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Mon Oct 26, 2009 5:27 pm 
Offline
Forum Members
Forum Members
User avatar

Joined: Fri Oct 02, 2009 2:28 pm
Posts: 183
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


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Sat Dec 12, 2009 3:39 am 
Offline
Power Poster
Power Poster

Joined: Mon Jun 12, 2006 9:02 pm
Posts: 1900
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";


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Mon Jan 18, 2010 12:36 am 
Offline
Power Poster
Power Poster
User avatar

Joined: Wed Jan 02, 2008 11:39 pm
Posts: 2102
Location: Lewiston-Idaho
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


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Mon Mar 08, 2010 11:15 am 
Offline
Forum Members
Forum Members

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


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Mon Mar 08, 2010 2:50 pm 
Offline
Power Poster
Power Poster
User avatar

Joined: Wed Jan 02, 2008 11:39 pm
Posts: 2102
Location: Lewiston-Idaho
did you try my plugin? that what it does. :D


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Mon Mar 08, 2010 6:08 pm 
Offline
Forum Members
Forum Members

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


Yes, but it does not work for CGBlog


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Mon Mar 08, 2010 6:45 pm 
Offline
Power Poster
Power Poster

Joined: Mon Jun 12, 2006 9:02 pm
Posts: 1900
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.


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Tue Mar 30, 2010 5:57 am 
Offline
Power Poster
Power Poster
User avatar

Joined: Thu Mar 23, 2006 9:25 am
Posts: 1864
Location: The Netherlands
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


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Mon Jun 21, 2010 11:41 am 
Offline
Forum Members
Forum Members

Joined: Fri Oct 03, 2008 7:37 am
Posts: 11
I had the same problem.

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


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
 Post subject: Re: Previous and next article in news details page
PostPosted: Fri Oct 29, 2010 3:46 am 
Offline
Forum Members
Forum Members

Joined: Wed Jun 03, 2009 4:28 am
Posts: 22
This is cool can it be modified into CGBlog?


Top
  Profile  
 
Share On:
Share on Facebook Share on Twitter Share on Google+
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Hosting Nation - Managed CMSMS Hosting