2012-02-12 71 views
0

從一個單獨的站點上的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>"; 
} 
?> 

回答

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

?> 

注意,你叫table在你的foreach循環中,而且你也沒有指定該行的開始和結束,用tr

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

我試過這個,它打破了表格,並把所有的數據作爲一行文字 – zhaobaloth 2012-02-12 21:00:31

+1

試試我的解決方案 - 這是缺少的''和''.... – MrJ 2012-02-12 21:02:17

+0

我沒有看到它很抱歉 – 2012-02-12 21:04:36