Jump to content

[SOLVED] SimpleXML question (really a noob question - bigtime)


prime

Recommended Posts

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

 

 

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++;
}

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 :-)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.