2015-12-14 3 views
0

Я хочу отобразить список категорий с соответствующими форумами, как и каждый классический форум. 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

Вы можете использовать условный 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']; 
} 
Смежные вопросы