表头重复

I am a new programmer and I don't have enough knowledge in programming that's why I am here seeking for help. I've been using this code from weblesson tutorial and this works well. The output of this code is a menu tab with 3 tabs (GF, 2F, 3F (that is for storeylevel)) Inside of respective tabs is the rooms under the storey level selected. This query returns the output well but with some issues.

What I have been struggling to is, the table header duplicates for every returned row value. What changes in this code should I do to avoid duplicated headers and maintain the header once only?

All help will be highly appreciated. Thank you so much.

$tab_query = "SELECT * FROM rooms GROUP BY level_id ASC";
$tab_result = mysqli_query($connect, $tab_query);
$tab_menu = '';
$tab_content = '';
$i = 0;
while($row = mysqli_fetch_array($tab_result))
{
if($i == 0)
{
$tab_menu .= '
<li class="active"><a href="#'.$row["level_id"].'" data-toggle="tab">'.$row["level_id"].'</a></li>
';
$tab_content .= '
<div id="'.$row["level_id"].'" class="tab-pane fade in active">
';
}
else
{
$tab_menu .= '
<li><a href="#'.$row["level_id"].'" data-toggle="tab" >'.$row["level_id"].'</a></li>
';
$tab_content .= '
<div id="'.$row["level_id"].'" class="tab-pane fade">
';
}
$product_query = "SELECT * FROM rooms WHERE level_id = '".$row["level_id"]."'";
$product_result = mysqli_query($connect, $product_query);

while($sub_row = mysqli_fetch_array($product_result))
{
$tab_content .= '

<table>
<tr>
<td>
Name
</td>
</tr>
<tr>
<td>
'.$sub_row["rm_name"].'
</td>
</tr>
</table>
';
}
$tab_content .= '<div style="clear:both"></div></div>';
$i++;
}

To display the tab menu and tab content values, I simply echo the two variables inside a div.