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

simplexml and smarty foreach loops

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
blackhawkmx
Smarty Regular


Joined: 20 May 2005
Posts: 76

PostPosted: Tue Oct 14, 2008 12:59 am    Post subject: simplexml and smarty foreach loops Reply with quote

There is a easy to understand way to get all element names and values from an xml document tree...

Code:

<contact>
  <extrainfo>
     <phone>123456789</phone>
     <ext>no-ext</ext>
     <address info>smarty avenue</address>
     <state>AA</state>
     <city>cit of lights</city>
     <zip>12345</zip>
  </extrainfo>
  <name>blackhawk</name>
  <email>blackhawk@smarty.net</email>
  <url>www.myspace.com</url>
  <comment>connecting simple xml with smarty foreach</comment></contact>


with simplexml...
Code:

$sxe = simplexml_load_file($myxmlfile);
$sxecontent = $sxe->root;
$children = $sxe->children();         
   foreach($children AS $child) {
   $element = dom_import_simplexml($child);
   $name = $element->nodeName;
   $collectsimple[] = array($name => $child);
   }
   
    if ($children) {
       $page ->assign("simplevalues",$collectsimple);
   }
   else {   
       $collectsimple = array(array("no fields found in xml" => ""));
       $page ->assign("simplevalues",$collectsimple);
    }


and then smarty!
Code:

{foreach from=$simplevalues|@array_reverse:true key=k item=i }
<p style="padding:15px 0 0 0;">
   {foreach from=$i key=key item=item}
    <h1>{$key}</h1>
         {foreach from=$item key=keysub item=itemsub}
         {if $keysub}<font color="#3888c0">{$keysub}</font>:{/if} {$itemsub}<br />
         <p style="padding-left:80px;">
               {foreach from=$itemsub key=keysubs item=itemsubs}
               {if $keysubs}{$keysubs}:{/if} {$itemsubs}<br />
               {/foreach}
         </p>
         {/foreach}
   {/foreach}
</p>
{/foreach}


not too much code at all to get all elements plus childs too! hope this helps someone out. Wink
_________________
Bring it back to basics. You and I can make it right!
Back to top
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Tue Oct 14, 2008 8:48 am    Post subject: Reply with quote

Many thanks for this useful informations!

<save thread to my bookmarks />

Makes some things a lot easier Smile ...
Back to top
View user's profile Send private message
anurdh65
Smarty n00b


Joined: 07 May 2009
Posts: 4

PostPosted: Thu May 07, 2009 10:04 am    Post subject: Reply with quote

Hi

This code is very useful for me
Back to top
View user's profile Send private message
roa123
Smarty Rookie


Joined: 26 Mar 2010
Posts: 12

PostPosted: Thu May 13, 2010 12:59 am    Post subject: re: Picking up a specific node? Reply with quote

Great code.. really helped me out.. How would i go picking up a particularly node? In your code it loops through all nodes and spits out both key and values, but I am after a specific one...

Thanks again for your help.
cheers,
Ritchie
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 -> Tips and Tricks 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