我想显示一个类别与其相应的论坛列表,就像每一个经典论坛。以下是一个例子。
我在互联网上找到了这段代码,它有点工作,但它不能正确地关闭表格。每个类别都应该有自己的表,现在只有一个</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>';发布于 2015-12-14 00:38:57
您也可以使用条件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'];
}https://stackoverflow.com/questions/34258127
复制相似问题