prime Posted November 21, 2007 Share Posted November 21, 2007 Hi I'm using php to parse a xml heres my code <table border="1" bordercolor="red" width="80%"> <?php $entries = simplexml_load_file('toollinks.xml'); foreach ($entries->link as $entry) { echo "<tr><td>"; echo "<a href=\"$entry->sitelink\">$entry->name</a>"; echo "</td> <td>"; echo "$entry->sitelink"; echo "</td></tr>"; } ?> </table> My question is, is there an easy way to sort this, I know this is probaly just a sort() but I'm new to xml fullstop and am unsure exactly where to use this. and my brain has been a fuse getting this far Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted November 21, 2007 Share Posted November 21, 2007 well there is no sorting the XML, You have to take the values from xml into arrays, then sort the arrays and then rebuild the xml here is some quick code to bring all the 'link' elements into an array and iterate thru them to get the sub-elements. $entries = simplexml_load_file('toollinks.xml'); $links = $entries->link; // points to first link element $i = 0; foreach ($links as $link) { // now $links becomes an iterator $linkarray[$i] = array('name'=>'','sitelink'=>''); $linkarray[$i]['name'] = (string)$link->name; $linkarray[$i]['sitelink'] = (string)$link->sitelink; $i++; } Quote Link to comment Share on other sites More sharing options...
prime Posted November 21, 2007 Author Share Posted November 21, 2007 then I just shuffle the arrays and do a foreach loop? that simple? Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted November 21, 2007 Share Posted November 21, 2007 yup Quote Link to comment Share on other sites More sharing options...
prime Posted November 21, 2007 Author Share Posted November 21, 2007 Thank you very much :-) I first started trying to fiund out about how to parse xml a,d I was first trying to make sense of the expat parser, which was giving me nightmares. this simple xml parser makes life a lot easier Thank you again :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.