如何使用嵌套循环生成 HTML table?
How to generate HTML table with nested loop?
我有一个 table 应该如下所示:
http://hell-rider.de/Fotos/img/beispiel-1.JPG
我有以下 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>";
然而,我的脚本的实际输出如下:
http://hell-rider.de/Fotos/img/beispiel2.JPG
我该如何更改我的脚本以正确填充第三列(而不是 MetallurgyTradeLaboratory OperationLaboratory OperationLaboratory OperationResearchCybernetics)?
<th>
标签用于 header 个单元格,您应该使用 <td>
个数据单元格。
$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++;
在我把它放在上面的地方,要么就在右大括号 [=15 的上面=].为了使其正常工作,$allgemeinxml->result->rowset->row
和 $xml->result->rowset->row
需要相同数量的元素。
我有一个 table 应该如下所示:
http://hell-rider.de/Fotos/img/beispiel-1.JPG
我有以下 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>";
然而,我的脚本的实际输出如下:
http://hell-rider.de/Fotos/img/beispiel2.JPG
我该如何更改我的脚本以正确填充第三列(而不是 MetallurgyTradeLaboratory OperationLaboratory OperationLaboratory OperationResearchCybernetics)?
<th>
标签用于 header 个单元格,您应该使用 <td>
个数据单元格。
$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++;
在我把它放在上面的地方,要么就在右大括号 [=15 的上面=].为了使其正常工作,$allgemeinxml->result->rowset->row
和 $xml->result->rowset->row
需要相同数量的元素。