tags:

views:

44

answers:

1

I'm trying to create "related articles" for our news, based on tags (single word tags). If a story is tagged with 3 tags, I'd like to be able to pull up the most recent entries for each tag, order them by date, and display only the most recent 5.

Here's what I'm trying, that obviously doesn't work, because it overwrites the array variable $pullRelated with the last tag it goes through. Since i got stuck there, I wasn't sure how to go about sorting all the entries by date and then only displaying the most recent 5. Any help is appreciated. Thanks!

    foreach ($tags as $t) {

    $pullRelated = mysql_query("SELECT * FROM posts WHERE MATCH(tags) AGAINST ('$t') AND status >= 2 AND newsID != $newsID ORDER by postDate DESC LIMIT 5");

    }

    while($related = mysql_fetch_array($pullRelated)) {
        echo $related['postTitle']." ".date("M j, Y",$related['postDate'])."<BR>";
}
+3  A: 

Why not use directly MySQL FULL TEXT index boolean mode?

$str = '';
foreach ($tags as $t) 
    $str .= '+'.$t.' ';

$q = mysql_query("SELECT * FROM posts WHERE MATCH(tags) AGAINST ('$str' IN BOOLEAN MODE) AND status >= 2 AND newsID != $newsID ORDER by postDate DESC LIMIT 5");

while($related = mysql_fetch_array($q))
    echo $related['postTitle']." ".date("M j, Y",$related['postDate'])."<BR>";
Benoit Vidis
Thanks for your reply. I wasn't aware of in boolean mode. To make it work for me, I didn't want the "+" between tags, that way ALL the tags didn't need to match, but only one. Thanks again!
scatteredbomb