0

I'm trying to retrieve data from an XML file using PHP and keep returning the same data in my xml file, regardless of what variable it is. I realise it should be in a loop.

I've been trying to get it to go into a loop but then the code doesn't seem to work. There doesn't seem to be any great examples on the web of combined if/else statements in a for loop. Anyone know of a quick work round? Thank you.

$xml=simplexml_load_file("images/database.xml") or die("Error: Cannot create object");
if ($word = 'Agitated') {
echo $xml->programme->mood['Agitated'] . "<br>";
echo $xml->programme->name . "<br>";
echo $xml->programme->description . "<br>";

}
else {
echo $xml->programme->mood['Calm'] . "<br>";
echo $xml->programme->name . "<br>";
echo $xml->programme->description . "<br>";
}
  • A cursory google search yielded a few pages with great examples of using for loops to navigate XML. Take the top result which is from here in SO [Loop through an XML object with SimpleXML](http://stackoverflow.com/questions/19561657/loop-through-an-xml-object-with-simplexml) – Luke Nov 21 '16 at 23:45
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – Luke Nov 21 '16 at 23:46

1 Answers1

1

You only have one equal sign (=) inside your if, you need two for comparison (==).

But your logic is wrong, if you want to loop, you need a while, for, foreach and any other iterator function

leoap
  • 1,684
  • 3
  • 24
  • 32