简体   繁体   English

PHP为3个数组的每个循环创建3个列表

[英]PHP for each loop for 3 arrays to create 3 column table

I am trying to create a table with HTML and PHP that displays a different array (array will eventually come from a DB table) in each column of the HTML/PHP table.我正在尝试使用 HTML 和 PHP 创建一个表,该表在 HTML/PHP 表的每一列中显示不同的数组(数组最终将来自数据库表)。 There are 3 Columns.有 3 列。

<table class="table-styling">

          <tr>
            <th>Column 1</th>
            <th>Column 2</th>
            <th>Column 3</th>
          </tr>
         
          <?php
          $arr = array(1, 2, 3, 4);
          foreach ($arr as $value) {
          echo '<tr>'.
               '<td>'.$value.'</td>';
          }

          $arr2 = array(5, 6, 7, 8);
          foreach ($arr2 as $value2) {
          echo '<td>'.$value2.'</td>';  
          } 

          $arr3 = array(9, 10, 11, 12);
          foreach ($arr3 as $value3) {
          echo '<td>'.$value3.'</td>'.'</tr>';  
          } 
          ?>

</table>

CSS CSS

.table-styling{
  border-collapse: collapse;
  width: 100%;
  color: #eb4034;
  font-family: monospace;
  font-size: 15px;
  text-align: left;
}
.table-styling th{
  background-color: #eb4034;
  color: white
}

Current Result当前结果

Column 1第 1 栏 Column 2第 2 栏 Column 3第 3 栏
1 1
2 2
3 3
4 4 5 5 6 6 7 7 8 8 9 9
10 10
11 11
12 12

Expected Result预期结果

Column 1第 1 栏 Column 2第 2 栏 Column 3第 3 栏
1 1 5 5 9 9
2 2 6 6 10 10
3 3 7 7 11 11
4 4 8 8 12 12

The issue is not about the database.问题不在于数据库。
You should check your logic about how you use for-loops.您应该检查有关如何使用 for 循环的逻辑。
Because the logic to use for-loops in your mind is wrong.因为在你脑海中使用 for 循环的逻辑是错误的。

Please let me explain to you why.请让我向你解释原因。

In the fourth row of your wrong result are在你的错误结果的第四行是
4 -- 5,6,7,8 -- 9. 4 -- 5,6,7,8 -- 9。
But why?但为什么? Let me explain to you.让我给你解释一下。

There are three loops in your code.您的代码中有三个循环。

At the end of the first loop, your code makes 4.在第一个循环结束时,您的代码变为 4。
In the second loop, your code makes 5,6,7,8.在第二个循环中,您的代码生成 5、6、7、8。
At the beginning of the third loop, your code makes 9.在第三个循环开始时,您的代码为 9。

And then echo to finish the fourth row.然后 echo 完成第四行。

This is why the fourth row shows 4 5 6 7 8 9.这就是为什么第四行显示 4 5 6 7 8 9。

You want to use 3 numbers of 1-dimension for-loop to show a 2-dimension table.您想使用 3 个一维 for 循环来显示二维表。
But this is a logical error in your thinking.但这是你思维的逻辑错误。
How your echo a table is by the 2-level for-loops (not 1-level for-loop). 2 级 for 循环(不是 1 级 for 循环)如何回显表。

For example, you may need 2-dimension array.例如,您可能需要二维数组。
And do some code like the following.并执行如下代码。

$myArray = array(
    array(1, 5, 9),
    array(2, 6, 10),
    array(3, 7, 11),
    array(4, 8, 12),
);

$out = "<table>";
foreach($myArray as $row) {
    $out .= "<tr>";
    foreach ($row as $cell) {
        $out .= "<td>" . $cell . "</td>";
    }
    $out .= "</tr>";
}
$out .= "</table>";


echo $out;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM