Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1] 2 3 4

Author Topic: Previous and Next article links  (Read 12105 times)

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Previous and Next article links
« on: August 31, 2009, 01:07:25 pm »

Original 1.6 mod modified for 1.7

0. Backup

1. Add this to your language file
Code: [Select]
#fast links
$l['previous_article'] = 'Previous Article: ';
$l['next_article'] = 'Next Article: ';

2. Add this new function
Code: [Select]
<?php

// Version 3
function fast_links() {
global $_ID$_catID;
if (!$_ID) return;
if (!$_catID) return;
$vis = !_ADMIN ' AND a.visible=\'YES\'' '';
$query '
(SELECT
"'
.l('previous_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM '
._PRE.'articles AS a
LEFT OUTER JOIN '
._PRE.'categories as c
ON a.category = c.id
LEFT OUTER JOIN '
._PRE.'categories as x
ON (c.subcat = x.id 
AND x.published = "YES")
WHERE position = 1
AND a.published = 1
AND a.category = '
.$_catID.'
AND c.published = "YES"
'
.$vis.'
AND (
CASE
WHEN a.artorder = (SELECT artorder FROM '
._PRE.'articles WHERE id = '.$_ID.')
THEN a.date > (SELECT date FROM '
._PRE.'articles WHERE id = '.$_ID.')
ELSE a.artorder < (SELECT artorder FROM '
._PRE.'articles WHERE id = '.$_ID.')
END
)
ORDER BY artorder DESC,date ASC
LIMIT 1)
UNION
(SELECT
"'
.l('next_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM '
._PRE.'articles AS a
LEFT OUTER JOIN '
._PRE.'categories as c
ON a.category = c.id
LEFT OUTER JOIN '
._PRE.'categories as x
ON (c.subcat = x.id 
AND x.published = "YES")
WHERE position = 1
AND a.published = 1
AND a.category = '
.$_catID.'    
AND c.published = "YES"
AND (
CASE
WHEN a.artorder = (SELECT artorder FROM '
._PRE.'articles WHERE id = '.$_ID.')
THEN a.date < (SELECT date FROM '
._PRE.'articles WHERE id = '.$_ID.')
ELSE a.artorder > (SELECT artorder FROM '
._PRE.'articles WHERE id = '.$_ID.')
END
)
'
.$vis.'

ORDER BY artorder ASC,date DESC
LIMIT 1)
'

$result mysql_query($query);
if (!$result || !mysql_num_rows($result)) return;
else {
echo '<div id="fast_links">';
while ($r mysql_fetch_assoc($result)) {
$r['xsef'] = is_null($r['xsef']) ? '' $r['xsef'].'/';
echo '<p>'.$r['ltext'].'
<a href="'
._SITE.$r['xsef'].$r['csef'].'/'.$r['asef'].'/" title="'.$r['title'].'">'.$r['title'].'</a>
</p>'
;
}
echo '</div>';
}


/* Version 2 
function fast_links() {
global $_ID, $_catID;
if (!$_ID) return;
if (!$_catID) return;
$vis = !_ADMIN ? ' AND a.visible=\'YES\'' : '';
$query = '
(SELECT
"'.l('previous_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM '._PRE.'articles AS a
LEFT OUTER JOIN '._PRE.'categories as c
ON '._PRE.'category = c.id
LEFT OUTER JOIN '._PRE.'categories as x
ON c.subcat = x.id 
AND x.published = "YES"
WHERE position = 1
AND a.published = 1
AND a.category = '.$_catID.'
AND c.published = "YES"
AND ( a.id < '.$_ID.' AND a.id >= 1 )
'.$vis.'
ORDER BY a.id DESC
LIMIT 1)
UNION
(SELECT
"'.l('next_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM '._PRE.'articles AS a
LEFT OUTER JOIN '._PRE.'categories as c
ON category = c.id
LEFT OUTER JOIN '._PRE.'categories as x
ON c.subcat = x.id 
AND x.published = "YES"
WHERE position = 1
AND a.published = 1
AND a.category = '.$_catID.'    
AND c.published = "YES"
AND ( a.id > '.$_ID.' AND a.id < ( SELECT MAX(id) FROM '._PRE.'articles ) )
'.$vis.'
ORDER BY a.id ASC 
LIMIT 1)
'; 
$result = mysql_query($query);
if (!$result || !mysql_num_rows($result)) return;
else {
echo '<div id="fast_links">';
while ($r = mysql_fetch_assoc($result)) {
$r['xsef'] = is_null($r['xsef']) ? '' : $r['xsef'].'/';
echo '<p>'.$r['ltext'].'
<a href="'._SITE.$r['xsef'].$r['csef'].'/'.$r['asef'].'/" title="'.$r['title'].'">'.$r['title'].'</a>
</p>';
}
echo '</div>';
}
}
*/

// version 1 - don't use
/*
function fast_links() {
global $_ID, $_catID;
if (!$_ID) return;
if (!$_catID) return;
$query = '
SELECT
"'.l('previous_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM articles AS a
LEFT OUTER JOIN categories as c
ON category = c.id
LEFT OUTER JOIN categories as x
ON c.subcat = x.id 
AND x.published = "YES"
WHERE position = 1
AND a.published = 1
AND a.category = '.$_catID.'
AND c.published = "YES"
AND a.id = ( '.$_ID.' - 1 )
LIMIT 2
UNION
SELECT
"'.l('next_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM articles AS a
LEFT OUTER JOIN categories as c
ON category = c.id
LEFT OUTER JOIN categories as x
ON c.subcat = x.id 
AND x.published = "YES"
WHERE position = 1
AND a.published = 1
AND a.category = '.$_catID.'    
AND c.published = "YES"
AND a.id = ( '.$_ID.' + 1 )
LIMIT 2'; 
$result = mysql_query($query);
if (!$result || !mysql_num_rows($result)) return;
else {
echo '<div id="fast_links">';
while ($r = mysql_fetch_assoc($result)) { 
$r['xsef'] = is_null($r['xsef']) ? '' : $r['xsef'].'/';
echo '<p>'.$r['ltext'].'
<a href="'._SITE.$r['xsef'].$r['csef'].'/'.$r['asef'].'/" title="'.$r['title'].'">'.$r['title'].'</a>
</p>';
}
echo '</div>';
}
}
*/

?>


3. Add it to where you want it displayed. I have mine before the paginator in center
Code: [Select]
fast_links();
if (!empty($currentPage) && ($num> $article_limit) && $on) {
paginator( $currentPage, $totalPages, l('paginator'));
}

fin
« Last Edit: August 19, 2010, 10:27:51 am by nukpana »
Logged

skian

  • Full Member
  • ***
  • Karma: 14
  • Posts: 120
Re: Previous and Next article links
« Reply #1 on: August 31, 2009, 04:55:08 pm »

Hello,

Unlike the original mod, you use
Code: [Select]
a.id = ( '.$_ID.' - 1 )
and
Code: [Select]
a.id = ( '.$_ID.' + 1 )
to find the next/previous article.

This code will not work if article id +1 or -1 has been deleted or unpublished.

You should fix it to return a.id > $_ID and a.id < $ID (and adding an appropriate ORDER-BY statement)
Logged

Joost

  • Guest
Re: Previous and Next article links
« Reply #2 on: August 31, 2009, 06:26:32 pm »

You should fix it to return a.id > $_ID and a.id < $ID (and adding an appropriate ORDER-BY statement)

And add LIMIT 1
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: Previous and Next article links
« Reply #3 on: September 01, 2009, 12:35:18 am »

Updated.
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Previous and Next article links
« Reply #4 on: September 01, 2009, 10:02:12 am »

Updated.
Me too.
In former version "Previous Article" and  "Next Article" were printed without links.
There nothing is echoed. ???

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: Previous and Next article links
« Reply #5 on: September 01, 2009, 11:52:00 am »

Updated.
Me too.
In former version "Previous Article" and  "Next Article" were printed without links.
There nothing is echoed. ???

So the former didn't work ok
But now this one doesn't?  I don't understand...
« Last Edit: September 01, 2009, 11:54:22 am by nukpana »
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Previous and Next article links
« Reply #6 on: September 01, 2009, 12:22:21 pm »

Fully workin on yer side?
Okey the issue is on my side.
Gonna check...
I'll be back.

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: Previous and Next article links
« Reply #7 on: September 01, 2009, 04:48:55 pm »

Yes, I worked off a fresh install with test articles mixed up.  Is it the same issue with the new code?  If you echo the query and run it in phpmyadmin, what results do you get?
Logged

Sven

  • ULTIMATE member
  • ******
  • Karma: 88
  • Posts: 2029
  • Chasing MY bugs!
    • hiseo.fr - rédacteur Web
Re: Previous and Next article links
« Reply #8 on: September 01, 2009, 05:47:06 pm »

Is it the same issue with the new code?
No: for some reasons I still don't get, query fails but I had the echo '<p>'.
Now nothing is printed. :o
If you echo the query and run it in phpmyadmin, what results do you get?
an error on line 6:
Code: [Select]
FROM '._PRE.'articles AS a
LEFT OUTER JOIN '._PRE.'categories as c
ON '
:-\
What the hell my sNews doin?

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: Previous and Next article links
« Reply #9 on: September 01, 2009, 06:00:57 pm »

Do you have the language lines copied over?


For the query:
before this line
$result = mysql_query($query);
type echo $query
Then go to a random article and copy the query


You should be seeing the previous article like the language lines above...

Then run it in phpmyadmin.
« Last Edit: September 01, 2009, 06:47:16 pm by nukpana »
Logged

Rui Mendes

  • Development,Testing, Support
  • Hero Member
  • *****
  • Karma: 195
  • Posts: 1009
  • sNews1.7
    • Comunidade Portuguesa
Re: Previous and Next article links
« Reply #10 on: September 02, 2009, 02:19:35 am »

Thanks nukpana to bring this mod to snews1.7

I made some changes and works for me:

Step 1: no changes
Step 2
Code: [Select]
<?php

function fast_links($first$last) {
global $_catID;
if (!$_catID) return;
$vis = !_ADMIN ' AND a.visible=\'YES\'' '';
$query '
(SELECT
"'
.l('previous_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM '
._PRE.'articles AS a
LEFT OUTER JOIN '
._PRE.'categories as c
ON a.category = c.id
LEFT OUTER JOIN '
._PRE.'categories as x
ON (c.subcat = x.id 
AND x.published = "YES")
WHERE position = 1
AND a.published = 1
AND a.category = '
.$_catID.'    
AND c.published = "YES"
AND ( a.id > '
.$last.' AND a.id <= ( SELECT MAX(id) FROM '._PRE.'articles ))
'
.$vis.'
ORDER BY a.id ASC 
LIMIT 1)
UNION
(SELECT
"'
.l('next_article').'" as ltext,
a.title, a.seftitle AS asef,
c.seftitle AS csef,
x.seftitle AS xsef
FROM '
._PRE.'articles AS a
LEFT OUTER JOIN '
._PRE.'categories as c
ON a.category = c.id
LEFT OUTER JOIN '
._PRE.'categories as x
ON (c.subcat = x.id 
AND x.published = "YES")
WHERE a.position = 1
AND a.published = 1
AND a.category = '
.$_catID.'
AND c.published = "YES"
AND ( a.id < '
.$first.' AND a.id >= 1 )
'
.$vis.'
ORDER BY a.id DESC
LIMIT 1)
'

$result mysql_query($query);
if (!$result || !mysql_num_rows($result)) return;
else {
echo '<div id="fast_links">';
while ($r mysql_fetch_assoc($result)) {
$r['xsef'] = is_null($r['xsef']) ? '' $r['xsef'].'/';
echo '<p>'.$r['ltext'].'
<a href="'
._SITE.$r['xsef'].$r['csef'].'/'.$r['asef'].'/" title="'.$r['title'].'">'.$r['title'].'</a>
</p>'
;
}
echo '</div>';
}
}
?>



Step 2a: find this line
Quote
$infoline = $r['displayinfo'] == 'YES' ? true : false;

and add this 2 lines after
Quote
$last = empty($last) ? $r['aid'] : $last;
$first = $r['aid'];


« Last Edit: September 02, 2009, 02:39:21 am by Rui Mendes »
Logged
Need a Job on Europe. Linkdin - Facebook / Group

Joost

  • Guest
Re: Previous and Next article links
« Reply #11 on: September 02, 2009, 04:52:17 am »

A few remarks:

1) I expect no additional functionality from:
a.id <= ( SELECT MAX(id) FROM articles ))
and
AND a.id >= 1

2) Next and previous is unrelated to `artorder` and `date`, as it is an id based ORDER BY.


Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: Previous and Next article links
« Reply #12 on: September 02, 2009, 10:39:19 am »

A few remarks:

1) I expect no additional functionality from:
a.id <= ( SELECT MAX(id) FROM articles ))
and
AND a.id >= 1

2) Next and previous is unrelated to `artorder` and `date`, as it is an id based ORDER BY.




1. Taking that gives me the same  functionality... odd. In the previous tests, the list would loop back (ie at the last article, it would give me the first as the next article)

2.  You just had to say so.  artorder, date, then id?
Logged

Joost

  • Guest
Re: Previous and Next article links
« Reply #13 on: September 02, 2009, 11:15:47 am »

2.  You just had to say so.  artorder, date, then id?

Articles are ordered as followed, when displayed in their respective categories:

ORDER BY artorder ASC,date DESC

post update

This is still not safe:
AND  a.id < $id
AND a.id > $id
What if an article is update with a new timestamp or was posted in the future?
If that's the case, there might be no relationship with the id at all.
It remains tricky  :-\


« Last Edit: September 02, 2009, 11:39:25 am by Joost »
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: Previous and Next article links
« Reply #14 on: September 02, 2009, 11:57:36 am »

Articles are ordered as followed, when displayed in their respective categories:

ORDER BY artorder ASC,date DESC
Thanks I will update the code shortly.

post update

This is still not safe:
AND  a.id < $id
AND a.id > $id
What if an article is update with a new timestamp or was posted in the future?
If that's the case, there might be no relationship with the id at all.
It remains tricky  :-\

First, Wouldn't a.published = 2 be for future publishing? If so, wouldn't the a.published = 1 cover that?
Secondly, we could do something like this: - (untested and me thinking from the top of my head)
AND (a.id > $_ID || a.date > (select date from articles where id = $_ID))
Logged
Pages: [1] 2 3 4