|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
blackhawkmx Smarty Regular
Joined: 20 May 2005 Posts: 76
|
Posted: Tue Oct 14, 2008 12:59 am Post subject: simplexml and smarty foreach loops |
|
|
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. _________________ Bring it back to basics. You and I can make it right! |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Tue Oct 14, 2008 8:48 am Post subject: |
|
|
Many thanks for this useful informations!
<save thread to my bookmarks />
Makes some things a lot easier ... |
|
Back to top |
|
anurdh65 Smarty n00b
Joined: 07 May 2009 Posts: 4
|
Posted: Thu May 07, 2009 10:04 am Post subject: |
|
|
Hi
This code is very useful for me |
|
Back to top |
|
roa123 Smarty Rookie
Joined: 26 Mar 2010 Posts: 12
|
Posted: Thu May 13, 2010 12:59 am Post subject: re: Picking up a specific node? |
|
|
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 |
|
|
|
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
|