【问题标题】:loop categories with assigned forums带有指定论坛的循环类别
【发布时间】:2009-08-15 20:37:25
【问题描述】:

论坛猫: id、名称
论坛: id、名称、cat_id

我怎样才能将它们连接在一起并打印分配给它们下的类别的论坛 如果有人能帮我一把,我会很感激的

这就是我的意思:

一个类别

论坛

论坛


第二类

另一个论坛

另一个论坛

另一个论坛


第三类

另一个论坛

另一个论坛

另一个论坛

$reslt = mysql_query("select id, name, cat_id from forums");

while ($row=mysql_fetch_assoc($reslt)) {

 echo "<h1>Category here</h1<";
 echo "<h3>$row[name]</h3>";

}

【问题讨论】:

    标签: php sql mysql


    【解决方案1】:

    你想要这个查询:

    SELECT forums.*, forumcats.name AS category_name
    FROM forums
    INNER JOIN forumcats ON (forums.cat_id = forumcats.cat_id)
    

    然后,当您循环查看结果时,您想要确定何时进入新类别。例如:

    $last_cat_id = null;
    while ($row=mysql_fetch_assoc($reslt)) {
      if ($last_cat_id != $row['cat_id']) {
        echo '<h1>' . $row['category_name'] . '</h1>';
        $last_cat_id = $row['cat_id'];
      }
      echo "<h3>$row[name]</h3>";
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多