-2

Me and my friend are trying to make a webpage which has a flash game on it. There's only going to be one page for all the games so we're using XML to load data about the game. Before I got started I did some simple test but they're not working. I'm trying to print the title of the first game. This is the XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<games>
    <game id="0">
        <name>3d Worm</name>
        <source>games/3dworm.swf</source>
    </game>

    <game id="1">
        <name>Portal</name>
        <source>random_source</source>
    </game>
</games>

This is the PHP file:

<?php
require("details.php");
$xml = simplexml_load_file("games.xml");
$games = $xml->games;
$game = $games->game[0];
echo $game->name;
?>

When I open the page it's just blank and I don't know why.

DJRyan
  • 149
  • 2
  • 10
  • 1
    This needs basic debugging. Does `$xml` contain anything? Does `$games` contain anything? Is error reporting turned on (e.g. `error_reporting(E_ALL);`)? – Pekka Feb 18 '12 at 11:36
  • I'd strongly recommend investing (time and/or money) in a good IDE with debugging capabilities. You'll never look back as it can really help see what;s going on in situations like this. – liquorvicar Feb 18 '12 at 11:38
  • it is `$games = simplexml_load_file('games.xml');` – Gordon Feb 18 '12 at 11:39
  • possible duplicate of [Change text of root SimpleXML Element](http://stackoverflow.com/questions/6819306/change-text-of-root-simplexml-element) – Gordon Feb 18 '12 at 11:39
  • Sorry both $xml and $games are objects but $game is null. – DJRyan Feb 18 '12 at 11:41
  • `$xml` is `` so there is no `$xml->games`. When you do `$xml->games` you are adding a new Games node, e.g. ``. See my answer in the duplicate. – Gordon Feb 18 '12 at 11:44
  • possible duplicate of [A simple program to CRUD XML](http://stackoverflow.com/questions/4906073/a-simple-program-to-crud-node-and-node-values-of-xml-file) – Gordon Feb 18 '12 at 11:48

1 Answers1

1
$games = simplexml_load_file("games.xml");
foreach($games->game as $game) {
  print $game->name . "\n";
}
Gordon
  • 312,688
  • 75
  • 539
  • 559
busypeoples
  • 737
  • 3
  • 6