简体   繁体   English

将php数组填充到HTML表中

[英]Populate php array into HTML table

Populate php array into HTML table 将php数组填充到HTML表中

I have data in a php array (say, $rows2) that gives the below output in key-value pairs. 我有一个php数组(比如$ rows2)中的数据,它以键值对的形式给出了下面的输出。 I am trying to format this data into an HTML table: 我正在尝试将此数据格式化为HTML表格:

0 => Month:
1 => 07/2011,
2 => 7-8
3 => / 1432 A.H
4 =>  
5 => Location:
6 => Plainfield,
7 => NJ,
8 => USA
9 => Calculation Method:
10 => Value 10
11 =>  
12 => Juristic Method:
13 => Standard 

I tried to use the below code to populate the table: 我尝试使用以下代码填充表格:

echo '<table border=1>';
while (list($key, $value) = each($rows2)) {

    echo $value2.$value;
        if($i%5 == 0){
            echo '<tr><td>'.$value2.'</td></tr>';

        }
        $value2='';
    $i++;
}
echo '</table>';

I get the below output: 我得到以下输出:

--------------------------------------------------------
Month:      |   07/2011     |   7-8     |   1432 A.H
--------------------------------------------------------
Location:   |   Plainfield  |   NJ      |   USA
--------------------------------------------------------
Calculation |   ValueX      |  Juristic |   ValueY
Method:     |               |  Method:  |
--------------------------------------------------------

I am trying to split the last row into 2 rows and then colspan the second to look like the below result. 我试图将最后一行拆分为2行,然后将第二行拆分为下面的结果。

--------------------------------------------------------
Month:      |   07/2011     |   7-8     |   1432 A.H
--------------------------------------------------------
Location:   |   Plainfield  |   NJ      |   USA
--------------------------------------------------------
Calculation |           ValueX      
Method:     |           
--------------------------------------------------------
Juristic    |           ValueY
Method:     |               
--------------------------------------------------------

Could someone please help? 有人可以帮忙吗? Thanks in advance. 提前致谢。

If the following things are true: 如果以下情况属实:

  1. The number of rows will always be the same 行数总是相同的
  2. The number of columns will always be the same 列数始终相同

Then you're probably better off just inserting parts of the array right into the table, like so: 然后你可能最好只是将数组的部分插入表中,如下所示:

<table>
<tr>
    <td><?= $array[0]?></td>
    <td><?= $array[1]?></td>
    <td><?= $array[2]?></td>
    <td><?= $array[3]?></td>
</tr>
<tr>
    <td><?= $array[9]?></td>
    <td colspan='3'><?= $array[10]?></td>
</tr>
<tr>
    <td><?= $array[12]?></td>
    <td colspan='3'><?= $array[13]?></td>
</tr>
</table>
<table>
<?php
foreach( $row in $rows2) { 
    echo '<tr>';
    echo '<td>' . $row[0] . '</td>';
    echo '<td>' . $row[1] . '</td>';
    echo '<td>' . $row[2] . '</td>';
    echo '<td>' . $row[4] . '</td>';
    echo '</tr><tr>';
    echo '<td>' . $row[5] . '</td>';
    echo '<td>' . $row[6] . '</td>';
    echo '<td>' . $row[7] . '</td>';
    echo '<td>' . $row[8] . '</td>';
    echo '</tr><tr>';
    echo '<td>' . $row[9] . '</td>';
    echo '<td colspan="3">' . $row[10] . '</td>';
    echo '</tr><tr>';
    echo '<td>' . $row[12] . '</td>';
    echo '<td colspan="3">' . $row[13] . '</td>';
    echo '</tr>';
} // end foreach
?>
</table>

I think it would be easier if you just mixed the HTML and PHP together instead of trying to do it in a loop like what you're doing. 我认为如果你只是将HTML和PHP混合在一起而不是像你正在做的那样在循环中尝试它会更容易。

echo "<table>
<tr>
    <td>{$arr[0]}</td>
    <td>{$arr[1]}</td>
    <td>{$arr[2]}</td>
    <td>{$arr[3]}</td>
</tr>
<tr>
    <td>{$arr[9]}</td>
    <td colspan='3'>{$arr[10]}</td>
</tr>
<tr>
    <td>{$arr[12]}</td>
    <td colspan='3'>{$arr[13]}</td>
</tr>
</table>";

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

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