duanjia1870 2011-09-21 07:30 采纳率: 0%
浏览 24
已采纳

mySQL + php,在循环中向另一个表中的标记显示相关帖子

Ok, so I have 5 tags on my front page, I want to show 5 comments related to each tag, the tags are stored in one table, and the comments in another. I have a field in the comments table called "relatedID" wich is equal to the tags table id column.
the tags are sorted by the 5 highest ranked tags from the last 20 entries into the table.

My query:

$q = mysql_query("SELECT * FROM (SELECT * FROM tags WHERE countrycode='$lang[LANGUAGE]' ORDER BY timedate DESC LIMIT 20) tmp, comments WHERE comments.related = '1' && comments.lang = '$lang[LANGUAGE]' && comments.relatedID = tmp.id ORDER BY tmp.rank ASC LIMIT 5");

while($result = mysql_fetch_array($q)) { 
echo $result['tag'],'<br />';
echo $result['comment'],'<br />';
}

I want it to look like this:

Tag 1
comment 1
comment 2
comment 3
comment 4
comment 5

Tag 2
comment 1
comment 2
comment 3
comment 4
comment 5

Tag 3

And so on..

Any simple solution to this?

Now it only show up with the same tag and same 5 comments x 5 times.

  • 写回答

2条回答 默认 最新

  • doushengyou2617 2011-09-21 07:44
    关注

    Just make a second query:

    // Query tags
    $q = mysql_query("...");
    
    while($result = mysql_fetch_array($q)) { 
        echo $result['tag'],'<br />';
        // Query comments
        $q1 = mysql_query("SELECT * FROM comments WHERE ... LIMIT 5");
        while($result = mysql_fetch_array($q)) {
            echo $result['comment'],'<br />';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)