表头在xml数据表中重复

时间:2012-02-12 20:51:54

标签: php xml

我从一个单独的网站上的xml中提取一些数据并尝试在表格中显示该数据,我可以提取数据但是当我试图在每个行的表格中显示数据时,我得到了表格标题,现在我知道为什么会发生这种情况(因为每一行都会调用表头回显),但我无法看到如何修复它。

<?php
$url = "http://elcu.herobo.com/testarea/include/cd_catalog.xml";
$xml = simplexml_load_file($url);

foreach($xml->CD as $cd){
    echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead><tbody>";
    echo "<td width='25%'> ".$cd->TITLE."</td>";
    echo "<td width='25%'> ".$cd->ARTIST."</td>";
    echo "<td width='25%'> ".$cd->COMPANY."</td>";
    echo "<td width='25%'> ".$cd->PRICE."</td>";
    echo "</tbody></table>";
}
?>

2 个答案:

答案 0 :(得分:0)

我认为该表将在foreach循环之前到来,这样每条记录都会创建一行而不是一个表

 echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'><thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead><tbody>";

foreach($xml->CD as $cd)

  {




  echo "<td width='25%'> ".$cd->TITLE."</td>";

  echo "<td width='25%'> ".$cd->ARTIST."</td>";

  echo "<td width='25%'> ".$cd->COMPANY."</td>";

  echo "<td width='25%'> ".$cd->PRICE."</td>";


  }

 echo "</tbody></table>";

答案 1 :(得分:0)

你的意思是:

<?php
$url = "http://elcu.herobo.com/testarea/include/cd_catalog.xml";

$xml = simplexml_load_file($url);

echo "<table border='0' cellpadding='1' cellspacing='1' width'90%' id='1' class='tablesorter'>\n";
echo "<thead><tr> <th>Title</th> <th>Artist</th> <th>Company</th><th>Price</th></thead>";
echo "<tbody>";

foreach($xml->CD as $cd){
    echo "<tr>";
    echo "<td width='25%'> ".$cd->TITLE."</td>";
    echo "<td width='25%'> ".$cd->ARTIST."</td>";
    echo "<td width='25%'> ".$cd->COMPANY."</td>";
    echo "<td width='25%'> ".$cd->PRICE."</td>";
    echo "</tr>";
}
echo "</tbody>";
echo "</table>";

?>

请注意,您在foreach循环中调用了table,并且您没有使用tr

指定行的开头和结尾