So what I am trying to do is load data from an XML file into php and use those variables to generate content. For each item i want to get a new set of variable values and print them out onto the page. If there are 4 interfaceItems then it should print 4 displayWrappers with 4 unique titles. This isn’t working for me. Is there a better or more efficient way of doing this? The errors I am getting right now are:
Notice: Trying to get property of non-object
Warning: Invalid argument supplied for foreach()
<?php
$xmldata = simplexml_load_file('elements.xml');
foreach($xmldata->portfolio->interface->interfaceItem as $item) :?>
<?php
$title = ($item->title);
$desc = ($item->description);
$whatOne = ($item->whatOne);
$whatTwo = ($item->whatTwo);
$location = ($item->location);
?>
<div class="displayWrapper">
<div class="display">
<p> <?=$title ?> </p>
</div>
</div>
<?php endforeach;?>
<portfolio>
<interface>
<interfaceItem>
<title>modi tempora</title>
<decription>lorum ipsum</decription>
<whatOne> dolor sit amet</whatOne>
<whatTwo>sed quia non</whatTwo>
<location>i/blah.jpg</location>
</interfaceItem>
<interfaceItem>
<title>magnam aliquam</title>
<decription>omnis voluptas assumenda est, omnis dolor repellendus.</decription>
<whatOne>expedita distinctio</whatOne>
<whatTwo>possimus, omnis voluptas</whatTwo>
<location>i/blah2.jpg</location>
</interfaceItem>
</interface>
</portfolio>
I’m going to take a wild stab in the dark here and reduce it down to one or both of two possible problems.
Scenario 1
Your XML file looks like this
With SimleXML, the first element is the root node. You would need to change your code to use
Scenario 2
SimpleXML element iteration is case sensitive. If your XML looks like this
You would need to change your code to
Update
Given the XML sample in your question, if that is the contents of
elements.xml, it would simply be