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

报告相同问题?