PHP最近的论坛不显示某些论坛

时间:2010-09-03 22:38:12

标签: php mysql post hide forum

我已经为我网站主页上显示的最后5个帖子设置了一种方式。我想隐藏3个特定论坛(我查找了id,它们是24,25,35。以防万一)。我可以让他们隐藏,但是,它不会显示5个帖子。所以我希望能够显示最近的5个跳过这3个论坛,同时仍然显示下一个5.这也是一个phpBB论坛,这意味着任何事情。

下面是我的PHP代码。
    

 $recent = mysql_query("SELECT * FROM forum_posts ORDER BY post_time DESC LIMIT 5");
 while ($recent_row = mysql_fetch_assoc($recent)){
  //Get Data
  $post_id = $recent_row['post_id'];
  $topic_id = $recent_row['topic_id'];
  $forum_id = $recent_row['forum_id'];
  $poster_id = $recent_row['poster_id'];
  $post_time = $recent_row['post_time'];

  if ($forum_id != 24 && $forum_id != 25 && $forum_id != 35){
   //Get Topic Name
   $topic_name = mysql_query("SELECT topic_title FROM forum_topics WHERE topic_id='$topic_id'");
   $topic_name = mysql_fetch_assoc($topic_name);
   $topic_name = $topic_name['topic_title'];

   //Get Username
   $username = mysql_query("SELECT username FROM forum_users WHERE user_id='$poster_id'");
   $username = mysql_fetch_assoc($username);
   $username = $username['username'];

   echo "<a href = 'http://www.ravenwolfguild.net/forum/memberlist.php?mode=viewprofile&u=$poster_id' class = 'LinkStyle'>$username</a> posted in \"".
   "<a href = 'http://www.ravenwolfguild.net/forum/viewtopic.php?f=$forum_id&t=$topic_id&p=$post_id#p$post_id' class = 'LinkStyle'>$topic_name</a>\"<br>";
  }
 }
?>

2 个答案:

答案 0 :(得分:1)

看起来您事先知道要拒绝的ID。 然后在第一个查询中执行此操作 -

$recent = mysql_query("SELECT * FROM forum_posts where forum_id NOT IN (24, 25, 35) ORDER BY post_time DESC LIMIT 5"); 

仍然提取5个结果,不包括不需要的

以下是整合查询,它执行与3次查询相同的任务

$recent = mysql_query("SELECT fposts.username FROM forum_posts as fposts INNER JOIN forum_topics AS ftopics ON (ftopics.topic_id = fposts.topic_id)
INNER JOIN forum_users AS fusers ON (fusers.user_id = forum_posts.poster_id)

WHERE fposts.forum_id NOT IN (24, 25, 35) ORDER BY fposts.post_time DESC LIMIT 5");

while ($recent_row = mysql_fetch_assoc($recent))
{
     echo "$username posted in \"". "$recent_row['username']\""; 
}

答案 1 :(得分:0)

一个查询中的所有内容:

$query = "
SELECT *, 
(SELECT ft.topic_title FROM forum_topics ft WHERE ft.topic_id= fp.topic_id) topic_title, 
(SELECT fu.username FROM forum_users fu WHERE fu.user_id= fp.poster_id) username 
FROM fp.forum_posts 
where fp.id not in (24, 25, 35) 
group by fp.forum_id 
ORDER BY fp.post_time DESC 
LIMIT 5";