【问题标题】:multiple foreach loops in html tablehtml表中的多个foreach循环
【发布时间】:2015-06-28 06:39:33
【问题描述】:

在我的数据库中,我有 2 个表。

轴台:

 ______________________________________
| axle_id | train_id | axle | distance |  
|_________|__________|______|__________|   
|   1     |     1    |   1  |    20    |
|   2     |     1    |   2  |    50    |
|   3     |     1    |   3  |   200    |
|   4     |     1    |   4  |    50    |
|   5     |     1    |   5  |   200    |
|   6     |     1    |   6  |    50    |
|   7     |     1    |   7  |   200    |
|   8     |     1    |   8  |    50    |
|_________|__________|______|__________|

转向架:

 ___________________________________________
| bogie_id | axle_id | train_id | bogie_nr |  
|__________|_________|__________|__________|   
|    1     |    1    |    1     |    1     |
|    2     |    2    |    1     |    1     |
|    3     |    3    |    1     |    2     |
|    4     |    4    |    1     |    2     |
|    5     |    5    |    1     |    3     |
|    6     |    6    |    1     |    3     |
|    7     |    7    |    1     |    4     |
|    8     |    8    |    1     |    4     |
|__________|_________|__________|__________|

现在我想在我的页面上以这样的表格显示这些结果:

 _____________________
| bogie_nr | axle    |  
|__________|_________|   
|    1     |    1    |
|    1     |    2    |
|    2     |    3    |
|    2     |    4    |
|    3     |    5    |
|    3     |    6    |
|    4     |    7    |
|    4     |    8    |
|__________|_________|

我试着用这段代码来做这个:

<table id="end_result_axle_bogies">
    <tr>
        <th>Train Axles</th>
        <th>Train Bogies</th>
    </tr>

    <!--Show the end result!-->
    <?php
        $show_end_table = $database->axles($_GET['train_id']);
        $show_end_table2 = $database->bogies($_GET['train_id']);
        foreach($show_end_table as $end_axle_table){ 
            echo "<tr>" . "<td>" . $end_axle_table['axle'] . "</td>";
        }
        foreach($show_end_table2 as $end_axle_table2){ 
            echo "<td>" . $end_axle_table2['bogie_nr'] . "</td>" . "</tr>";
        } 
    ?>
</table>

输出有效(显示正确信息)。但是桌子不好用。它向我展示了这个结果:

(将文本设为绿色以查看 div 是否正常工作。确实如此)。

有人知道如何让桌子正常工作吗?

【问题讨论】:

  • 更改 foreach。就像你现在做的那样,它是 1 2...在一个 foreach 中回显所有内容是更好的方法。

标签: php html css sql


【解决方案1】:

试试这个:

<?php
    $show_end_table = $database->axles($_GET['train_id']);
    $show_end_table2 = $database->bogies($_GET['train_id']);
    $table2 = "";
    foreach($show_end_table2 as $end_axle_table2){ 
        $table2[] = $end_axle_table2['bogie_nr'];
    } 
    foreach($show_end_table as $key => $end_axle_table){
        echo "<tr>";
        echo "<td>" . $end_axle_table['axle'] . "</td>";
        echo "<td>" . $table2[$key] . "</td>";
        echo "</tr>";
    }
?>

更改 foreach。就像您现在所做的那样,它是 &lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;... 在一个 foreach 中回显是更好的方法。

【讨论】:

  • $end_axle_table2 在这里未定义,因为它不在 foreach 中,可以修复吗??
【解决方案2】:

您可以尝试使用 MySQL 视图 (info here),然后在 foreach 循环中遍历 1 个视图,而不是执行 2 个数据库调用。

【讨论】:

    【解决方案3】:

    试试这个:

    从转向架表中获取所有 bogie_nr 和相应的 axis_id
    然后像这样进行for循环

    foreach(bogie_nr as bogie)
     <tr>
         <td> show bogie_nr </td>
         <td> Fetch axle for respective axle_id of bogie_nr from axle table
         </td>
     </tr>
     end for loop
     </tr>
    

    【讨论】:

      【解决方案4】:

      下面的代码可能对你有用....

      <table id="end_result_axle_bogies">
      <tr>
          <th>Train Axles</th>
          <th>Train Bogies</th>
      </tr>
      
      <!--Show the end result!-->
      <?php
          $show_end_table = $database->axles($_GET['train_id']);
          $show_end_table2 = $database->bogies($_GET['train_id']);
          foreach($show_end_table as $end_axle_table){ 
              echo "<tr>" . "<td>" . $end_axle_table['axle'] . "</td>";
          }
      </table>
      <table>
      foreach($show_end_table2 as $end_axle_table2){ 
              echo "<tr><td>" . $end_axle_table2['bogie_nr'] . "</td>" . "</tr>";
          } 
      ?>
      </table>
      

      【讨论】:

      • 稍作改动,现在显示 2 个表格。但如果可能的话,我想把它们放在一张桌子上
      猜你喜欢
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多