1

I have a dynamic banner with images/info that are saved in an XML file. I'm adding the option for the user to rearrange the images however they like (Jquery sortable) and once I get the new order (which countains the id attribute) I would like to rearrange without having to copy each node's infos and saving it again in the new order. For instance:

<banners>
 <banner id="1001">
  <infos...>
 </banner>
 <banner id="1003">
  <infos...>
 </banner>
 <banner id="900">
  <infos...>
 </banner>
</banners> 

Say the user rearranged to 900, 1003, 1001. I would like to be able to get the nodes by the ID# and reposition them accordingly. Is there an easy way of doing that?

Fabi
  • 973
  • 7
  • 18

1 Answers1

0

(1) add a child order to every <banner>-node and (2) sort the xml by that child.

for (1): $xml is the simplexml-object, the order is stored in $order...

$order=array(900,1001,1003);

foreach ($xml->banner as $banner) {

    foreach ($order as $k => $v)
        if ($banner['id']==$v) $banner->addChild('order',$k);

}

for (2): sort xml by order:
see accepted answer in this post: sorting the table fields using simple XML and xpath

see live-demo @ http://codepad.viper-7.com/MdcT7B

Community
  • 1
  • 1
michi
  • 6,565
  • 4
  • 33
  • 56
  • thank you, I was just curious to see if addChild would carry over the whole from the banner with id=X. I will give this answer a try and post it on here in case it works. – Fabi Apr 23 '13 at 21:34