Retrieving XML nodes using IDs, but in the XML order

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jizepi
Forum Newbie
Posts: 3
Joined: Wed Oct 21, 2009 6:54 am

Retrieving XML nodes using IDs, but in the XML order

Post by jizepi »

This is a pretty obscure case, so pls bear with me.

I have an XML file which is essentially a site map, containing details of pages on my site, with a unique ID for each page.

Users can browse the site and add pages of interest to a 'print basket', which writes the ID for each page into a cookie (pipe-separated), so the cookie value could look like this: "1|12|3|28|14" for example. Each page ID is appended to the end of the list in the cookie, so they appear in the order in which the user has added them.

I have a page which retrieves and displays the content from all the pages the user has added, but I need to find a way of getting the pages in the order in which they appear in the XML, not the order they appear in the cookie (so the structure of the retrieved content makes sense).

One solution would be to iterate through every node in the XML, and check whether the ID is in the cookie or not, but this seems pretty inefficient, particularly if there are lots of pages in the XML.

Is there a way to take my unordered list of IDs (split into an array), and sort them into the same order in which they appear in the XML structure?

Thanks,

Mike
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Retrieving XML nodes using IDs, but in the XML order

Post by markusn00b »

Why not split the data up via the pipe separator (explode()), and then order the array using sort().

Let us know how you get on,
Mark.
jizepi
Forum Newbie
Posts: 3
Joined: Wed Oct 21, 2009 6:54 am

Re: Retrieving XML nodes using IDs, but in the XML order

Post by jizepi »

I've split the data into an array, and I can sort it, but I don't know how to get it to sort into the same order the ID's appear in the XML. The page ID's won't necessarily be sequential, but I need the pages to be output according to the XML structure.

Mike
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Retrieving XML nodes using IDs, but in the XML order

Post by markusn00b »

jizepi wrote:I've split the data into an array, and I can sort it, but I don't know how to get it to sort into the same order the ID's appear in the XML. The page ID's won't necessarily be sequential, but I need the pages to be output according to the XML structure.

Mike
Ah, then maybe you should have an order_id in the XML that is also stored in this array (think 2D). Then you can order the array based on that.
jizepi
Forum Newbie
Posts: 3
Joined: Wed Oct 21, 2009 6:54 am

Re: Retrieving XML nodes using IDs, but in the XML order

Post by jizepi »

Yeah, I'd thought about that, but it would make the maintenance pretty tricky, particularly when adding in new pages (for example, in between two other pages which had consecutive order_id values; you'd have to manually change all the subsequent ones) -- I really wanted the XML structure to do the work for me, and not have to think about adding ordering numbers.

Mike
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Retrieving XML nodes using IDs, but in the XML order

Post by markusn00b »

jizepi wrote:Yeah, I'd thought about that, but it would make the maintenance pretty tricky, particularly when adding in new pages (for example, in between two other pages which had consecutive order_id values; you'd have to manually change all the subsequent ones) -- I really wanted the XML structure to do the work for me, and not have to think about adding ordering numbers.

Mike
I'm dumb at the moment / incredibly tired.

Any body else want to chime in?
Post Reply