Home
+ · sNews Development
+++ · [MOD] related articles
posted in sNews Development | on 09.06.2008. 19:03
Hey, here is the Related Article mod i use here on my site
this mod ported from mod by baker http://snewscms.com/forum/index.php?topic=4045.0

place this code in snews.php or external file,
then call the function on your index.php.
to form relation betwin articles:
use Keywords META Tag field, enter word you like.   
<?php
//related arts
function retrieve_related($column$table$field$value$n$id) {
    
$query "SELECT $column FROM ".db('prefix')."$table WHERE ($value) AND id <> '$id' ORDER BY id DESC LIMIT $n ";
    
$result mysql_query($query) or die(mysql_error().' <br />'.$query);
    
$retrieve = array();
    while (
$r mysql_fetch_array($result)) {$retrieve[] = $r[$column];}
    return 
$retrieve;
}

function 
rel_articles(){
    
$article get_id('article');
    if (!empty(
$article)) {
        echo 
'<div class="box">';
        
$id retrieve('id','articles','seftitle',$article);
        
$keywords retrieve('keywords_meta','articles','seftitle',$article);
        
$kws explode(","$keywords);
        
$num_kw count($kws);   
        if (
$kws[0] != '') {
            for (
$i 0$i $num_kw$i++) {
                
$keywords trim($kws[$i]);
                
$value .= " keywords_meta LIKE '%$keywords%' ";
                if (
$i $num_kw-1) {
                    
$value .= " OR ";
                }
            }
            
$get_art retrieve_related('seftitle''articles''keywords_meta'$value,'5',$id);
            
$num_art count($get_art);
            echo 
"<ul class="relt">n";
            echo 
"<li>Related Articles: </li>n";
            if(
$num_art>0) {
                for (
$i 0$i $num_art$i++) {
                    
$art_id $get_art[$i];
                    
$query "SELECT * FROM ".db('prefix')."articles WHERE seftitle = '$art_id' AND published=1";
                    
$result mysql_query($query);
                    while (
$r mysql_fetch_array($result)) {
                        
$home s('home_sef');
                        
$cat $r['category'];
                        
$categorySEF retrieve('seftitle','categories','id',$cat);
                        
$categorySEF = empty($categorySEF) ? $home $categorySEF;
                        echo 
'<li><a href="'.db('website').$categorySEF.'/'.$r['seftitle'].'/" title="'.$r['title'].'">'.$r['title'].'</a></li>'."n";
                    }
                }
            }
            else {
                echo 
l('no_relation');
                }
            echo 
'</ul>'."n";
        }
        else {
            echo 
l('no_relation');
            }
        echo 
'</div>';
    }
}
?> 
Comments
writen by funlw65
on 2008-06-09 20:57:19
Definitely, is not Henrich's mod. his mod is based on category, your mod is based on related keywords... your is true related mode. I suspected this and is the reason why I asked about it. Thank you!
writen by moshe ••
on 2008-06-09 21:38:24
you welcome my friend :)
writen by funlw65
on 2008-08-09 01:48:52
Hi Moshe!

Today I installed your mod and is working great, thank you!

I removed those "n" chars and replaced one line of code. It was a translation mistake, or you wanted to check if someone is using your code?
writen by moshe ••
on 2008-08-09 08:07:08
yes i think this right thing to do,
i forgot to remove them.

what line did you replace ?
writen by funlw65 •••
on 2008-08-09 15:04:57
I replaced this: echo "<ul class="relt">n"; With this: echo '<ul>'; But I think it must be: echo '<ul class="relt">';
writen by funlw65 ••••
on 2008-08-09 15:12:03
And optimized this:
$query = "SELECT * FROM ".db('prefix')."articles WHERE seftitle = '$art_id' AND published=1";

like this:
$query = "SELECT title,seftitle,category FROM ".db('prefix')."articles WHERE seftitle = '$art_id' AND published=1";

Of course, there are only 5 queries, so does not matter so much.

For my package I'm using "the control panel" made for @Henrich's mod...
writen by moshe •••••
on 2008-08-09 23:11:43
as you said, there are only 5 queries haha
anyway i will fix my code with your fixes :)
thank you my friend
writen by funlw65
on 2008-08-24 15:50:39
Hi Moshe,
I found another issue. If you let the function to look also in tags of static pages, somewhere you will have a link pointing to a yourdomain.com/home/your_homepage/ and this will be reported as identical content with yourdomain.com/

What you think?

Awesome day
writen by moshe ••
on 2008-08-24 20:29:33
hey vasi :)
yes, you are right, i see the problem,
will have to consider the use of homeless uri mod ?

i dont use pages very much, i just use articles and cats.

will work on it :)

awesome evening my friend.
writen by moshe •••
on 2008-08-25 21:37:49
hey vasi,
after some testing with v16,
i think to my self,
Pages Do have home in the url,
thats the standart for pages is it ?
only if one has moded snews to work
without home for pages.

if i make a page, i can not access it without home as category.

what do you think ?

awesome evening my friend :)
writen by funlw65 ••••
on 2008-08-26 00:44:06
Hi Moshe,
You get me wrong and is my fault because i did not explained well.

Look at this case:
------------------
On my blog, I have a static page defined as homepage. Normally, this page reside in home category. But, because is set as home page, you can access it with this link: yourdomain.com/ . Of course, you know you can access it also with yourdomain.com/home/yourpage/ . But only you know that :P not Google. And in your site, this link (yourdomain.com/home/yourpage/) does not exist. So, your page, because is home page, can be accessed only this way : yourdomain.com/ . And because of this, no problem with duplicate content.

Until your mod will display a link to this page (as related article) under an article with same tags as your homepage. And that link will be displayed as yourdomain.com/home/yourpage/ . In that moment, your "secret" link will be revealed to google bot and he will say: Gotcha! Duplicate content.

That is the reason I said your related articles mod must search for tags only in articles. And this is the case also with tag clouds of Equilni.

P.S.My category for static pages is 'page' :) but I have same problem when I set a static page as homepage... But I use a htaccess redirection :P

My friend, right?
writen by moshe •••••
on 2008-08-26 11:20:48
aahhhaa :)
good morning vasi,
yes, i have an option for you to try :)

please change this query line:
$query = "SELECT * FROM ".db('prefix')."articles WHERE seftitle = '$art_id' AND published=1";

to:
$query = "SELECT * FROM ".db('prefix')."articles WHERE seftitle = '$art_id' AND published=1 AND position='1' AND category!='0'";

it exclude the home and static pages out of the results.

let me know if it what you pointed :)
writen by funlw65 ••••••
on 2008-08-26 18:22:04
Yes Moshe, is working but I put AND position = 1 inside retrieve_related, to exclude static pages from start.
writen by moshe •••••••
on 2008-08-26 20:24:44
that is an option as well :)
i added it to the rel_articles function beacose
i use the retrieve_related function in different
places and or functions.

any way its do its job is good by me hehe :)

awesome evening my friend
writen by funlw65 ••••••••
on 2008-08-26 23:37:57
Thank you Moshe,
This mod is working great, I'm content. I saw that you "parametrized" also the name of tables... which made me think how you use it (excluding actual destination of function) :P !

Good evening to you too.
writen by moshe •••••••••
on 2008-08-27 22:02:47
oo yea, this function serves me with projects im
working on, that includes more db tables, like subcat,
or multi sub categories, and if i remmber correctly,
i use it with snews 17, maybe a version of it, i cant remmber exactly hehe.