I have an XML file and i want to read data with PHP. I had read data from it but the problem is that i only want to run loop for one category and show all matches in this category Here is my XML File link And My PHP code
<?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>
In your first foreach you can access the id (or name) of your category through the attributes() function.
http://us1.php.net/manual/en/simplexmlelement.attributes.php
$cate->attributes()->id
That should help you figure out which category you're currently iterating and then just show your desired and/or skip the others.
Output of an XML-file requires just 3 lines of PHP code, if you use XSLT as transformation language to 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
?>
This is the proper XSLT code for your HTML table (file "test.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>
This does not contain all your columns, but it gives the idea. I admit this is not the way you wanted to do it, but it solves your problem and shows how easy it is to use XSLT :-)