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 Link to comment https://forums.phpfreaks.com/topic/78183-solved-simplexml-question-really-a-noob-question-bigtime/ 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++; } Link to comment https://forums.phpfreaks.com/topic/78183-solved-simplexml-question-really-a-noob-question-bigtime/#findComment-395696 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? Link to comment https://forums.phpfreaks.com/topic/78183-solved-simplexml-question-really-a-noob-question-bigtime/#findComment-396198 Share on other sites More sharing options...
BenInBlack Posted November 21, 2007 Share Posted November 21, 2007 yup Link to comment https://forums.phpfreaks.com/topic/78183-solved-simplexml-question-really-a-noob-question-bigtime/#findComment-396203 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 :-) Link to comment https://forums.phpfreaks.com/topic/78183-solved-simplexml-question-really-a-noob-question-bigtime/#findComment-396234 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.