dougou5852 2015-04-22 21:08
浏览 92
已采纳

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

I have a table that should look like the following:

http://hell-rider.de/Fotos/img/beispiel-1.JPG

I have the following PHP script:

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>";

The actual output of my script is the following, however:

http://hell-rider.de/Fotos/img/beispiel2.JPG

How do I have to change my script to populate the third column correctly (and not with MetallurgyTradeLaboratory OperationLaboratory OperationLaboratory OperationResearchCybernetics)?

  • 写回答

1条回答 默认 最新

  • douqianni4080 2015-04-22 21:24
    关注

    <th> tags are for header cells, you should be using <td> data cells.

    $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>";
    

    I don't know exactly what the use of if($row{'refTypeID'} == 42){ is, so you either want $type_index++; in the place I've put it above, or just above the closing brace }. For this to work well, $allgemeinxml->result->rowset->row and $xml->result->rowset->row need the same number of elements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作