Php 如何使用嵌套循环生成HTML表?

Php 如何使用嵌套循环生成HTML表?,php,foreach,html-table,Php,Foreach,Html Table,我有一张如下所示的桌子: 我有以下PHP脚本: foreach ($xml->result->rowset->row as $row){ if($row{'refTypeID'} == 42){ echo '<tr>'; echo "<th>"; echo $row{'date'}; echo "</th>"; echo "<th>";

我有一张如下所示的桌子:

我有以下PHP脚本:

foreach ($xml->result->rowset->row as $row){
    if($row{'refTypeID'} == 42){
        echo '<tr>';
        echo "<th>";
        echo $row{'date'};
        echo "</th>";
        echo "<th>";
        echo '' . $row{'ownerName1'} .'';
        echo "</th>";
        echo "<th>";
        foreach ($allgemeinxml->result->rowset->row as $type){
            echo '' . $type{'typeName'} .'';
        }
        echo "</th>";
        echo "<th>";
        echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
        echo "</th>";
        echo "<th>";
        echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
        echo "</th>";
        echo '</tr>';
    }
}
echo "</table>";
但是,我的脚本的实际输出如下所示:

如何更改脚本以正确填充第三列,而不是使用冶金贸易实验室操作实验室操作实验室操作实验室操作研究控制论?

标记用于标题单元格,您应该使用数据单元格

$types = array();

foreach ($allgemeinxml->result->rowset->row as $type){
    $types[] = $type{'typeName'};
}

$type_index = 0;

foreach ($xml->result->rowset->row as $row){
    if($row{'refTypeID'} == 42){
        echo '<tr>';
        echo "<td>";
        echo $row{'date'};
        echo "</td>";
        echo "<td>";
        echo '' . $row{'ownerName1'} .'';
        echo "</td>";
        echo "<td>";
        echo $types[$type_index];
        echo "</td>";
        echo "<td>";
        echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
        echo "</td>";
        echo "<td>";
        echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
        echo "</td>";
        echo '</tr>';
    }
    $type_index++;
}
echo "</table>";

我不知道if$row{'refTypeID'}==42{的确切用法是什么,所以您要么想要$type_index++;放在我放在上面的地方,要么就放在右大括号}的上面。为了让它正常工作,$allgemeinxml->result->rowset->row和$xml->result->rowset->row需要相同数量的元素。

所以你想让它说控制论,而不是冶金贸易实验室操作实验室操作实验室操作研究控制论?是的,每个单元格都有新的文本图片,但每个单元格都显示相同的文本,这不是你想要的吗?你想让每个单元格在gegenstand标题下都有不同的内容吗?@Geuthur很抱歉,你的问题让我几乎无法理解。我已尽我所能对其进行了编辑,以使您的实际问题更清楚,并鼓励您提供更好的答案。请检查我的编辑是否保留了您的原始意思,否则不要害怕大叫。@helmbert我制作了一张图片[link]sry my bad,当我更改时,它不是数组{然后当我使用脚本时不起作用,然后我得到了这个警告:array_pop希望参数1是array,对象在第150行的/var/customers/webs/ni7378_5/Eve/test.php中给出,它的XML格式是ithink@Geuthur哦,对不起,我弄错了。如果你把$allgemeinxml->result->rowset->row转储到某处,它会给你什么输出?知道对象是必需的。`objectSimpleXMLElement7 1{[@attributes]=>array14{[transactionDateTime]=>string19 2015-04-22 10:02:17[transactionID]=>string10 3898981967[quantity]=>string1[typeName]=>string10[typeID]=>string4 3409[price]=>string9 358749.99[clientID]=>string8 95203460[clientName]=>string23 Jackmerius Tackheritrix[stationID]=>string8 60003760[stationName]=>string46 Jita IV-Moon 4-Caldari海军装配厂[transactionType]=>string3购买[transactionFor]=>string8个人[…]及更多非常感谢:我为我的php语言学习了一个新字符串^^