开发者

loading from an XML file into PHP to generate content

开发者 https://www.devze.com 2023-03-06 15:15 出处:网络
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.

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('ele开发者_Python百科ments.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

<portfolio>
    <interface>
        <interfaceItem>

With SimleXML, the first element is the root node. You would need to change your code to use

foreach ($xmldata->interface->interfaceItem as $item)

Scenario 2

SimpleXML element iteration is case sensitive. If your XML looks like this

<root>
    <Portfolio>
        <Interface>
            <InterfaceItem>

You would need to change your code to

foreach ($xmldata->Portfolio->Interface->InterfaceItem as $item)

Update

Given the XML sample in your question, if that is the contents of elements.xml, it would simply be

foreach ($xmldata->interfaceItem as $item)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号