Page 1 of 1

PHP and XML

Posted: Thu Apr 15, 2010 2:38 am
by Yanayaya
Hello everyone,

I have created a page in PHP that uses cURL to parse XML data from another website. I was wondering if there is anyway to sort the results of the returned data? To make this clear I have included my source code. As you will no doubt immediately notice this is code to recieve XML data from the Warcraft Armory.

Code: Select all

                //fetches an xml document using the cURL library
	function fetchXML($url)
	{
		//initialize library
		$ch = curl_init();

		//used to make me look like a browser
		$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";

		//set the url
		curl_setopt ($ch, CURLOPT_URL, $url);

		//set that I want to wait for a response
		curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

		//make me look like a web browser
		curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);

		//get my data
		$data = curl_exec($ch);

		//clean up
		curl_close($ch);

		///return xml data
		return $data;
	}
		define("USE_CURL", true);

	//set my guild and realm info
	$realm = "Agamaggan";
	$guild = "Ministry+of+Silly+Walks";
	$url = "http://eu.wowarmory.com/guild-info.xml?r=" . $realm . "&n=" . $guild ;

	//get xml doc with character info
	$data = fetchXML($url);

	//create a SimpleXML object to parse the xml
	$guild_xml = new SimpleXmlElement($data);	
Then I have the following code in the body of my page to display my data.

Code: Select all

     foreach ($guild_xml->guildInfo->guild->members->character as $char) 
     {
     echo "\n<tr><td><a href=\"http://eu.wowarmory.com/character-sheet.xml?r=Agamaggan&cn={$char['name']}\">{$char['name']}</a></td><td>{$char['level']}</td></tr>";
     }
As you can see it simply creates rows in a table and put the returned data into those rows. What I would like to be able to do is sort the users by name alphabetically and possibly by rank or level. I was wondering if there was an easy way to accomplish this?

Many thanks in advance.

Re: PHP and XML

Posted: Thu Apr 15, 2010 3:07 am
by Bind
well the easiest way is to use a pre-coded sortable tables javascript and have it all done client side.

alternately, PHP has a large number of array sorting functions