Create array using loop for related items
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 ea开发者_开发问答ch 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>";
}
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>";
精彩评论