duanli3277 2015-12-14 00:31
浏览 23
已采纳

使用论坛显示论坛类别

I want to display a list of categories with their corresponding forums, like every classic forum. Heres an example.

I found this piece of code on the internet, it kinda works but it doesnt close tables properly. Every category should have their own table, right now it only closes with one </table>

// 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>';
  • 写回答

1条回答 默认 最新

  • dstm2014 2015-12-14 00:38
    关注

    You can use your conditional if ($lastCatID <> $row['cid']) to also know when to close/open a new table. You will also want to check that it is not the 1st table, so that you don't close it on the first check.

    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'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题