0

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>";
}

1 Answer 1

3

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>";
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.