Plings XML Sorter
From Plings Info
A function that takes parsed Plings XML data and sorts it according to selected fields with that data. As used in ShowMyPlings
<?php # Copyright (c) 2009 David Carpenter <david@substance.coop> # Released as free software under the MIT license, # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. /*Takes parsed plings (activity or venue) XML and sorts it by the given parameter*/ //To get the parsed data: //$Plings_API_url='http://feeds.plings.net/xml.activity.php/1/la/00EY?APIKey=5A605302-FC5BC526-4473C6AB-B4D9009B-RQMKMLDM'; //$xml = simplexml_load_file($Plings_API_url); //$originalactivities = $xml->activities->activity; //Example usage: //$activities = sort_plings_xml($originalactivities,'name','desc']); /*The results are automatically orderd alphabetically, and then by time*/ /*To reverse the order supply 'desc' as the third parameter in this function*/ function sort_plings_xml($originalactivities,$term,$order='asc') { //Set up some arrays that we can sort the data by... $start=array(); $sortby =array(); $activities=array(); //Loop through the XML grabbing the info we want and pushing this into new arrays foreach($originalactivities as $activity){ switch ($term) { case 'name': array_push ($sortby, htmlspecialchars(stripslashes($activity->Name))); break; case 'VenueName': array_push ($sortby, htmlspecialchars(stripslashes($activity->venue->Name))); break; case 'ProviderName': array_push ($sortby, htmlspecialchars(stripslashes($activity->provider->Name))); break; case 'cost': array_push ($sortby, htmlspecialchars(stripslashes($activity->Cost))); break; case 'la'; array_push($sortby, htmlspecialchars(stripslashes($activity->venue->LAName))); break; case 'ward'; array_push($sortby, htmlspecialchars(stripslashes($activity->venue->WardName))); break; } //start time array_push($start, htmlspecialchars(stripslashes($activity->Starts))); // echo $start[$j]; //Make an $activities array cos I don't understand the $xml array! array_push($activities, $activity); } //Debug and experiments //print_r($sortby); //print_r($start); //$start = array_reverse($start); //If order is 'desc' then once sorted we reverse the array - but this means we get the data sorted by e.g. Name alphabetically reversed, and //with the start time showing the last instance to the first - so we probably want, first instance to the last... //Reverse the order of the start times.... if ($order == 'desc') { $start=array_reverse($start); } //We've got our arrays now, so lets sort it //Sorts $activities by our term, then by time - cool, array_multisort($sortby, $start,$activities); //print_r($activities); //re-order our acticvities array descending if required... if ($order == 'desc') { $activities=array_reverse($activities); } //This is for checking... /* $sorted=array(); foreach ($activities as $activity){ array_push($sorted, $activity->Name.$activity->Starts); } print_r($sorted); */ //Done! return $activities; } //Example usage... //Get and Parse the XML $Plings_API_url='http://feeds.plings.net/xml.activity.php/1/la/00EY?APIKey=5A605302-FC5BC526-4473C6AB-B4D9009B-RQMKMLDM'; $xml = simplexml_load_file($Plings_API_url); $originalactivities = $xml->activities->activity; //Sort by activity name.. $sorted=sort_plings_xml($originalactivities,'name'); $list=array(); foreach ($sorted as $activity){ array_push($list, $activity->Name.' '.$activity->Starts); } print_r($list); //Sort by activity Cost - descending $sorted=sort_plings_xml($originalactivities,'cost','desc'); $list=array(); foreach ($sorted as $activity){ array_push($list, $activity->Cost.' '.$activity->Starts); } print_r($list); //Sort by VenueName $sorted=sort_plings_xml($originalactivities,'VenueName'); $list=array(); foreach ($sorted as $activity){ array_push($list, $activity->venue->Name.' '.$activity->Starts); } print_r($list); //Sort by ProviderName - descending $sorted=sort_plings_xml($originalactivities,'ProviderName','desc'); $list=array(); foreach ($sorted as $activity){ array_push($list, $activity->provider->Name.' '.$activity->Starts); } print_r($list); ?>

