I think you want something like this:
<?php
$db = Db::getInstance();
$stra= $db->query("SELECT * FROM `str-table`");
?>
<?php while ($str = $stra->fetch_array()) : ?>
<div>
<table id="homeTable">
<thead>
<th><?php echo $str['str']; ?></th>
</thead>
<?php $substr = $db->query("SELECT * FROM `substr-table` WHERE str_id = ".$str['str_id']); ?>
<?php while ($sub = $substr->fetch_array()) : ?>
<tr>
<td>
<?php echo $sub['substr']; ?>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php endwhile; ?>
The first SQL query gets all of the categories from the str-table.
The foreach loop then loops over them and prints a table for each one.
Inside the table, the second SQL query gets all of the subcategories from the substr-table for the current "str" category in the outer loop. It then prints a table row and a table cell for each substr category.
Let me know if this is what you needed. :)
Update with Model-View-Controller separated
Model:
<?php
$db = Db::getInstance();
$categories = array();
$stra= $db->query("SELECT * FROM `str-table`");
while ($str = $stra->fetch_assoc()) {
$substr = $db->query("SELECT * FROM `substr-table` WHERE str_id = ".$str['str_id']);
$subcategories = array();
while ($sub = $substr->fetch_assoc()) {
$subcategories[] = $sub;
}
$categories[] = array_merge($str, array('substrs' => $subcategories));
}
return $categories;
?>
Same as above, the first SQL query gets the categories from the str-table, and the second SQL query gets the corresponding subcategories for each row from the substr-table. The categories and their subcategories are then added to a multidimensional array which is then returned to the calling code.
The controller can get the categories-array from the model and pass it to the view for display.
View:
<?php foreach ($categories as $category) : ?>
<div>
<table id="homeTable">
<thead>
<th><?php echo $category['str']; ?></th>
</thead>
<?php foreach ($category['substrs'] as $subcategory) : ?>
<tr>
<td>
<?php echo $subcategory['substr']; ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<?php endforeach; ?>