Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

smarty simplexml show elements with IDs

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Palumba
Smarty n00b


Joined: 06 Feb 2015
Posts: 3

PostPosted: Fri Feb 06, 2015 2:29 pm    Post subject: smarty simplexml show elements with IDs Reply with quote

I have an XML-Feed with the following structure:

<id>12</id>
<author>asasasa</author>
<date>2013-01-31 12:20:48</date>
<reviewerAge>80</reviewerAge>
<subcategoryRatings>
<subcategoryRating>
<category id="price">Rates</category>
<rating scale="5">4</rating>

<comment/>
</subcategoryRating>
<subcategoryRating>
<category id="room">Rooms</category>
<rating scale="5">4</rating>
<comment/>
</subcategoryRating>
<subcategoryRating>
<category id="location">Location</category>
<rating scale="5">3</rating>
<comment/>
</subcategoryRating>
<subcategoryRating>
<category id="service">Service</category>
<rating scale="5">5</rating>
<comment/>
</subcategoryRating>
</subcategoryRatings>

With the following simplexml I'm able to display the "id","author" and "date" elements, but not the "category" and "rating" elements.

{$xml = simplexml_load_file('xml.file')}

{foreach from=$xml item=list name='outer' }
{foreach from=$list item=ex name='loop' }
{if !$smarty.foreach.outer.first}

<h1> [{$smarty.foreach.loop.iteration}] </h1>
{$ex->author}<br />
{$ex->id}<br />
{$ex->date}<br />

{/if}
{/foreach}
{/foreach}

How can I do this?

Thank you very much.

Greetings
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Feb 06, 2015 6:29 pm    Post subject: Reply with quote

That's because they aren't in the scope of your loops.
Your structure actually looks like this:
Code:
<element>
    <id>12</id>
    <author>asasasa</author>
    <date>2013-01-31 12:20:48</date>
    <reviewerAge>80</reviewerAge>
    <subcategoryRatings>
        <subcategoryRating>
            <category id="price">Rates</category>
            <rating scale="5">4</rating>
            <comment/>
        </subcategoryRating>
        <subcategoryRating>
            <category id="room">Rooms</category>
            <rating scale="5">4</rating>
            <comment/>
        </subcategoryRating>
        <subcategoryRating>
            <category id="location">Location</category>
            <rating scale="5">3</rating>
            <comment/>
        </subcategoryRating>
        <subcategoryRating>
            <category id="service">Service</category>
            <rating scale="5">5</rating>
            <comment/>
        </subcategoryRating>
    </subcategoryRatings>
</element>

To access "category", you need a few more loops in there.

Also, you're using deprecated foreach syntax.
Back to top
View user's profile Send private message
Palumba
Smarty n00b


Joined: 06 Feb 2015
Posts: 3

PostPosted: Fri Feb 06, 2015 6:51 pm    Post subject: Reply with quote

Hi Deamon,

thank you for your answer.
How can I do this?
Can you give me an example?

Greetings
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat Feb 07, 2015 3:33 am    Post subject: Reply with quote

Example of what? Foreach loop?
http://www.smarty.net/docs/en/language.function.foreach.tpl
Everything you need to know about it.
Back to top
View user's profile Send private message
Palumba
Smarty n00b


Joined: 06 Feb 2015
Posts: 3

PostPosted: Sat Feb 07, 2015 2:23 pm    Post subject: Reply with quote

Got it with:
{$ex->subcategoryRatings->subcategoryRating[0]->category}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP