表迭代与不同的str

I want to design a table that contains several columns, but the columns are not my problem yet. The problem is rows. These are mysql tables:

tables-sql

I want to iterate tables base on count of str-table and the rows base on count of substr's that contain same str_id. Every str has a table. In the rows of every table there are substr of the specified str. I hope you to understand what do I mean.

Here is my html and PHP code:

<?foreach ($stra as $str) { ?>
<div>

  <table id="homeTable">

    <thead>
    <th>str</th>
    </thead>

    <tr>

      <td>

       **********

      </td>

    </tr>

  </table>

</div>

**********I want to put substrs here dynamically iterate each row until str_id for them are same and after that , the new table iterate base on new subject and new substrs**********

And here is my SQL code:

$db = Db::getInstance();
  $stra= $db->query("SELECT * FROM str-table");
  $substr= $db->query("SELECT * FROM str-table");

what should I do?

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; ?>