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

 

 

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.