2013-11-25 38 views
0

我有一個XML文件,我想用PHP讀取數據。我已經從中讀取數據,但問題是,我只是想爲一類運行循環,並顯示所有匹配此類別中 這裏是我的XML File link和我的PHP代碼僅循環一個類別XML-PHP

<?php 
$test = simplexml_load_file('http://www.goalserve.com/samples/soccer_livescore.xml'); 
//echo $test->category->matches->match->localteam["name"]."<br>"; 
//echo $test->category->matches["date"]; 
?> 
<table border="1"> 
<tr>  
<td style="border:none;">Local Team </td> 
<td>Score </td> 
<td>Visitor team</td> 
<td>Status </td> 
<td>Date </td> 
<td>Time </td> 
<td>HT </td> 
<td class="hidden-result">Events</td> 
</tr> 
<?php 
    foreach ($test->category as $cate) { 
    foreach ($cate->children() as $matches) { 
    foreach ($matches->children() as $match) { 
    // print_r($match); 
    echo "<td>".$match->localteam["name"]."</td>"; 
    echo '<td><a href="javascript:MoreInformation('.$match["fix_id"].');"><img src="http://www.goalserve.com/images/arrow-down.gif" alt="More information" title="More information" id=I'.$match["fix_id"].'" name=I'.$match["fix_id"].' />'.$match->localteam["goals"].'-'.$match->visitorteam["goals"].'</a></td>'; 
    echo "<td>".$match->visitorteam["name"]."</td>"; 
    echo "<td>".$match["status"]."</td>"; 
    echo "<td>".$match["date"]."</td>"; 
    echo "<td>".$match["time"]."</td>"; 
    echo "<td>".$match->ht["score"]."</td>"; 
    ?> 
    <div class="hidden-result"> 
    <?php 
    foreach ($match->events->event as $t_event) { 

    if($t_event["type"]=="goal") { 
     ?> 
    <td class="hidden-result" id="<?php echo "R".$match["fix_id"]?>" name="<?php echo "R".$match["fix_id"]?>"><img src='images/1.gif' alt=""><?php echo $t_event["player"]."(".$t_event["team"].")" ?></td> 
    <?php 
    } 
    if($t_event["type"]=="yellowcard") {?> 
    <td class="hidden-result" id="<?php echo "R".$match["fix_id"]?>" name="<?php echo "R".$match["fix_id"]?>"><img src='images/3.gif' alt=""><?php echo $t_event["player"]."(".$t_event["team"].")" ?></td> 
    <?php 
    } 
    if($t_event["type"]=="redcard") {?> 
    <td class="hidden-result" id="<?php echo "R".$match["fix_id"]?>" name="<?php echo "R".$match["fix_id"]?>"><img src='images/2.gif' alt=""><?php echo $t_event["player"]."(".$t_event["team"].")" ?></td> 
    <?php 
    } 
    }?> 
    </div> 
    <?php 
    echo "</tr>"; 
}  
} 
} 
?> 
</table> 
+0

嗨,請更新你的問題並顯示你的XML的有效片段.. – michi

回答

1

輸出只需要3的PHP代碼行,如果使用XSLT作爲轉換語言HTML:

<?php 
$proc=new XsltProcessor; 
$proc->importStylesheet(DOMDocument::load("test.xsl")); //load XSL script 
echo $proc->transformToXML(DOMDocument::load("soccer_livescore.xml")); //load XML file and echo 
?> 

這是你的HTML表格的正確XSLT代碼(文件「的考驗。 xsl「):

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
     <html> 
      <head/> 
      <body> 
       <table> 
        <tbody> 
         <tr> 
          <th>Local team</th> 
          <th>Score</th> 
          <th>Visitor team</th> 
          <th>Status</th> 
          <th>Date</th> 
          </tr> 
         <xsl:apply-templates select="scores/category[@name='England: Premier League']/matches/match"/> 
        </tbody> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
    <xsl:template match="match"> 
     <tr> 
      <td><xsl:value-of select="localteam/@name"/></td> 
      <td><xsl:value-of select="ht/@score"/></td> 
      <td><xsl:value-of select="visitorteam/@name"/></td> 
      <td><xsl:value-of select="@status"/></td> 
      <td><xsl:value-of select="@formatted_date"/></td> 
     </tr> 
    </xsl:template> 
    <xsl:template match="*"/> 
</xsl:stylesheet> 

這並不包含所有列,但它提供了這個想法。我承認這不是你想要做到的方式,但它解決了你的問題,並且顯示了使用XSLT是多麼容易:-)