从xml数据填充表

时间:2017-11-07 10:09:53

标签: php html xml dom

我有这个xml,我想让xml数据自动填充HTML表格, 代码有效,但它在表格内容上重复了

这里是xml数据

<root>
<stats>
<item>
<day>2017-11-01</day>
<impressions>2192</impressions>
<money>1.96790003</money>
</item>
<item>
<day>2017-11-02</day>
<impressions>2824</impressions>
<money>3.208500033</money>
</item>
<item>
<day>2017-11-03</day>
<impressions>3680</impressions>
<money>3.321799981</money>
</item>
</stats>
<total>
<impressions>8696</impressions>
<money>8.498200044</money>
</total>
<filter>
<dateFrom>2017-11-01</dateFrom>
<dateTo>2017-11-03</dateTo>
<groupBy>day</groupBy>
<format>xml</format>
</filter>
</root>

我正在使用这个PHP代码来获取xml数据,但这段代码从整个xml数据中获取,这使得重复的字段表

<?php
$dom = new DOMDocument;

$dom -> load('http://example.com/' . $dateselected . '&dateTo=' . $dateselected2 . '&format=xml');

$day = $dom->getElementsByTagName('day'); 
$impressions = $dom->getElementsByTagName('impressions');



echo ( "<table>");

    foreach($day as $node1) {
    foreach($impressions as $node2) {


         echo '<tr>';
         echo "<td>". $node1 -> textContent . "<td>";
         echo "<td>". $node2 -> textContent . "<td>";
         echo "<td>". $node2 -> textContent  *0.5/1000 ."<td>";
         echo '</tr>';


    }
}
    echo( "</table>");
?>

任何人都可以提示如何解决这个问题吗? 谢谢

2 个答案:

答案 0 :(得分:0)

我们试试这个:

<?php

$xmlContent = '<root>
    <stats>
        <item>
            <day>2017-11-01</day>
            <impressions>2192</impressions>
            <money>1.96790003</money>
        </item>
        <item>
            <day>2017-11-02</day>
            <impressions>2824</impressions>
            <money>3.208500033</money>
        </item>
        <item>
            <day>2017-11-03</day>
            <impressions>3680</impressions>
            <money>3.321799981</money>
        </item>
    </stats>
    <total>
        <impressions>8696</impressions>
        <money>8.498200044</money>
    </total>
    <filter>
        <dateFrom>2017-11-01</dateFrom>
        <dateTo>2017-11-03</dateTo>
        <groupBy>day</groupBy>
        <format>xml</format>
    </filter>
</root>';

$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;

//$dom->load('http://example.com/' . $dateselected . '&dateTo=' . $dateselected2 . '&format=xml');
$dom->loadXML($xmlContent);

$xpath = new DOMXPath($dom);
$stats = $xpath->query('/root/stats/item');

echo "<table>";

/* @var DOMElement $stat */
foreach ($stats as $stat) {
    $day = $xpath->query('day', $stat)->item(0)->nodeValue;
    $impressions = $xpath->query('impressions', $stat)->item(0)->nodeValue;
    $money = $xpath->query('money', $stat)->item(0)->nodeValue;
    $money = $money * 0.5 / 1000;

    echo "<tr>";
    echo "<td>" . $day . "<td>";
    echo "<td>" . $impressions . "<td>";
    echo "<td>" . $money . "<td>";
    echo "</tr>\n";
}
echo "</table>";

答案 1 :(得分:0)

在XML中有3个日期和4个展示次数,因此您的代码包含两个foreach'es,使3x4 = 12个'date' - 'impression'字段组合。 您需要根据日期计数从0..2开始循环,并且不要错过TOTALs行的第3个索引:

<?php

$str =
"
<root>
<stats>
<item>
    <day>2017-11-01</day>
    <impressions>2192</impressions>
    <money>1.96790003</money>
</item>
<item>
    <day>2017-11-02</day>
    <impressions>2824</impressions>
    <money>3.208500033</money>
</item>
<item>
    <day>2017-11-03</day>
    <impressions>3680</impressions>
    <money>3.321799981</money>
</item>
</stats>
<total>
    <impressions>8696</impressions>
    <money>8.498200044</money>
</total>
<filter>
    <dateFrom>2017-11-01</dateFrom>
    <dateTo>2017-11-03</dateTo>
    <groupBy>day</groupBy>
    <format>xml</format>
</filter>
</root>
";

echo ( "<table style='border: inset'>");

$dom = new DOMDocument;
$dom -> loadXML($str);
$day = $dom->getElementsByTagName('day');
$impressions = $dom->getElementsByTagName('impressions');
$money = $dom->getElementsByTagName('money');

//  table header
echo '<thead>';
echo "<th>Day<th>";
echo "<th>Impressions<th>";
echo "<th>Money<th>";
echo '</thead>';

for ($i=0; $i < $day->length; $i++) {
    // data
    echo '<tr>';
    echo "<td>". $day[$i]->textContent . "<td>";
    echo "<td>". $impressions[$i]->textContent . "<td>";
    echo "<td>". $money[$i]->textContent . "<td>";
    echo '</tr>';
}

// totals row
echo '<tr style="background-color: lightgray">';
echo "<td>Total:<td>";
echo "<td>". $impressions[$day->length]->textContent . "<td>";
echo "<td>". $money[$day->length]->textContent . "<td>";
echo '</tr>';

echo( "</table>");

?>

Check live example