首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用论坛显示论坛类别

用论坛显示论坛类别
EN

Stack Overflow用户
提问于 2015-12-14 00:31:12
回答 1查看 52关注 0票数 0

我想显示一个类别与其相应的论坛列表,就像每一个经典论坛。以下是一个例子

我在互联网上找到了这段代码,它有点工作,但它不能正确地关闭表格。每个类别都应该有自己的表,现在只有一个</table>结束。

代码语言:javascript
复制
// Start the table here - outside the loop
echo '<table>'; 

// Here's how you track if you need the header
$lastCatID = -1;

// Now loop
foreach($query as $row)   
{     
// Only show cat header on condition
if ($lastCatID <> $row['cid']) {
    echo '
      <tr>  
        <th>' . $row['cat_name'] . '</th>
        <th>Latest Reply</th>
        <th>Topics</th> 
        <th>Posts</th> 
        </tr>';

    // Note that you've shows this header so you don't show it again.
    $lastCatID = $row['cid'];
}


// Now output the rest       
echo '<tr>';  
echo '<td width="55%">Link</a></td>';
echo '<td width="25%">User</td>';
echo '<td width="10%" align="center">23523</td>';
echo '<td width="10%" align="center">343235</td>';
echo '</tr>';

}

echo '</table>';
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-14 00:38:57

您也可以使用条件if ($lastCatID <> $row['cid'])来知道何时关闭/打开一个新表。您还需要检查它是否是第一个表,这样就不会在第一个检查时关闭它。

代码语言:javascript
复制
if ($lastCatID <> $row['cid']) {
  if($lastCatID != -1) { // if not the 1st cat, then close the last table and start a new table
    echo '
    </table>
    <table>';
  }
    echo '
      <tr>  
        <th>' . $row['cat_name'] . '</th>
        <th>Latest Reply</th>
        <th>Topics</th> 
        <th>Posts</th> 
        </tr>';

    // Note that you've shows this header so you don't show it again.
    $lastCatID = $row['cid'];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34258127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档