0

hello i will filter xml elements and will display one element with an specified identification like this:

<?php

$url11= 'http://api.steampowered.com/ISteamUserStats/GetSchemaForGame/v0002/?key=XXXXXXXXXXXXXXXXXX&appid=240&l=german&format=xml';
$data11 = file_get_contents($url11);
$xml = simplexml_load_string($data11);

$joke ="WIN_BOMB_PLANT";

foreach($xml->availableGameStats->achievements->achievement as $key => $value) {
    if($value->name->__toString() != $joke) {
    ?>
<img src="<?php echo $value->icon->__toString(); ?>" border="0" height="64" width="64" >
        <?php
    }
}
?>  

and here is the xml document:

-<game>
 <gameName>ValveTestApp260</gameName>
 <gameVersion>181</gameVersion>
   -<availableGameStats>
      -<achievements>
        -<achievement>
          <name>WIN_BOMB_PLANT</name>
          <displayName>Bombenleger</displayName>
          <icon>
          http://media.steampowered.com/steamcommunity/public/images/apps/240 
          /4711014791e4ae29408e8bc2fcae1ab61ca7c189.jpg
          </icon>
         </achievement>
        -<achievement>
          <name>BOMB_PLANT_LOW</name>
          <displayName>Boomala Boomala</displayName>
          <icon>
          http://media.steampowered.com/steamcommunity/public/images/apps/240
          /5f689c20656716f2a8bf67bd26f3f7846786bca5.jpg
          </icon>
         </achievement>
         <achievement>
         -Another things-
         </achievement>
       </achievements>
    </availableGameStats>
 </game>

But now its display every element. like this example: https://i.stack.imgur.com/BagDu.jpg

But i will that the script just display this specified one like this: https://i.stack.imgur.com/X2Oph.png

can help me anyone? thanks ahead .. srry for my bad english or bad english ...

ProJaCore
  • 42
  • 8
  • You need to reverse the condition of your `if`-statement: `if ($value->name->__toString() == $joke)` – michi Jun 01 '14 at 20:43

1 Answers1

0

Rather than iterating, you can use xpath() to select the node you need:

$xml = simplexml_load_string($x); // assume XML in $x

$icon = $xml->xpath("//achievement[name='WIN_BOMB_PLANT']/icon")[0];

Note that for the [0]in the end of line 2, you need PHP >= 5.4. If you are on an older version, update or do:

$icon = $xml->xpath("//achievement[name='WIN_BOMB_PLANT']/icon");
$icon = $icon[0];

Cast $icon to string if necessary.

See it working: https://eval.in/157598

michi
  • 6,565
  • 4
  • 33
  • 56
  • how i can set an variable into: $icon = $xml->xpath("//achievement[name='{$VAR}']/icon"); i have php 5.5 > – ProJaCore Jun 02 '14 at 17:16
  • @ProJaCore I suggest you follow the link in my answer above and experiment a bit with that code. Try the solution you posted in your comment, tweak a bit here and there, and be proud when you finally did it. This learning will stick with you and build your skill base through time. – michi Jun 02 '14 at 20:45