Tutorial :Sorting Prices from XML


I am trying to sort the results by prices of an xml with info related to hotels. I want to display and sort the data by prices, given by a value called minCostStay, so ASC and DESC are must to do this.

I am using foreach to display the data endlessly. What I need to do now is sort out these data by price asc and desc. I am pretty much a newbie coding, so any help will be appreciated, specially if it is a simple example =)

This is more or less what I am doing:

   $url ="http://www.somedomain.com/cgi/xml/engine/get_data.php?ref=$ref&checkin=$checkin&checkout=$checkout&rval=$rval&pval=$pval&country=$country&city=$city&lg=$lg";      // load SimpleXML  $all = new SimpleXMLElement($url, null, true);    foreach($all as $hotel) // loop through our hotels        {  

echo <<

<table width="100%" border=0>       <tr>      <td colspan="2"><h2><a href="{$hotel->rooms->room->bookUrl}">{$hotel->name}<span class="stars" widht="{$hotel->rating}">{$hotel->rating}</span></h2></a></a>      <p><b>Direccion:</b> <i>{$hotel->address}</p></i>      </td>      <td valign="middle"><div align="center"><a href="{$hotel->rooms->room->bookUrl}"><img src="{$hotel->photoUrl}"></div></td>    </tr>    <tr>      <td colspan="2"> $rest...<a href="{$hotel->rooms->room->bookUrl}"><strong>ampliar información</strong></a></td>        <td  colspan="2" valign="middle" align="right"><div align="center">PRECIO: <h3>{$hotel->currencyCode}{$hotel->minCostOfStay}</h3> </a></div></td>        </tr>    <tr>      <td colspan="2"><div align="center"><a href="{$hotel->rooms->room->bookUrl}"><strong>VER COMENTARIOS, FOTOS Y DETALLES DE ESTE HOTEL</strong></a></div></td>  <td colspan="2"><div align="center">$text</a></div></td>     </a></div></td>      </tr>        EOF;      echo '</table>';      }  

I´m hearing you!

Thanks in advance!


1.Get them from XML (you seem to be able to do that).
2.Put them in an array
3.sort() the array, if your items are more complex. possibly using usort():

$d =  new SimpleXMLElement('<root>    <item>      <price>1234</price>    </item>    <item>      <price>4123</price>    </item>    <item>      <price>3142</price>    </item>    <item>      <price>2431</price>    </item>  </root>');  $items = array();  foreach($d->item as $item) $items[] = $item;  function _mysort($a,$b){return $a->price - $b->price;}  usort($items,'_mysort');  var_dump($items);  


I recently disocvered SimpleDOM for a project where I needed a more advanced xpath system. I really like it and I think it could do what you're looking for. You can find it (docs are bundled in the source code download) here: http://code.google.com/p/simpledom/

You'll want to look at the sortedXPath() method I believe:

sortedXPath (line 888)    Run an XPath query and sort the result    This method accepts any number of arguments in a way similar to array_multisort()       1. // Retrieve all <x/> nodes, sorted by @foo ascending, @bar descending     2. $root->sortedXPath('//x', '@foo', '@bar', SORT_DESC);     3.      4. // Same, but sort @foo numerically and @bar as strings     5. $root->sortedXPath('//x', '@foo', SORT_NUMERIC, '@bar', SORT_STRING, SORT_DESC);        * access: public    void sortedXPath (string $xpath)        * string $xpath: XPath expression  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »