douhan9748 2018-08-11 11:37
浏览 60

表迭代与不同的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?

  • 写回答

1条回答 默认 最新

  • dongyong5912 2018-08-11 12:09
    关注

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

报告相同问题?

悬赏问题

  • ¥20 关于多单片机模块化的一些问题
  • ¥30 seata使用出现报错,其他服务找不到seata
  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection