使用论坛显示论坛类别

时间:2015-12-14 00:31:12

标签: php

我想显示一个类别列表及其相应的论坛,比如每个经典论坛。 Heres an example

我在互联网上找到了这段代码,它有点工作,但它没有正确关闭表格。每个类别都应该有自己的表格,现在它只用一个</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 个答案:

答案 0 :(得分:1)

您可以使用条件if ($lastCatID <> $row['cid'])来了解何时关闭/打开新表格。您还需要检查它不是第一张表,以便您在第一张支票时不要关闭它。

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'];
}