1

I am extracting data from an XML Engine using simplexml, and I am completely stuck when I try to oder ASC or DESC the data.

Be aware that I am not writing this values myself, but I am extracting them from another source.

Ive got another response to a similiar question and I had this as an example (which works):

$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);

The problem is that I don't know how to write the data into the root /item /price.... part automatically.

This is a hotel search engine some hotels pay to be first, but I want the user to be able to see the hotels by price ASC or DESC, so I think I should list the hotels on memory on the server (sorry, i lack the coder slang) and then order them by price and print them.

Any chance I can get some help?

This is what I am doing:

assigning a var to the ur

$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&sort=desc";

Creating my new SimpleXML

$all = new SimpleXMLElement($url, null, true);

Doing a loop to show all hotels:

foreach($all as $hotel) // loop through our hotels {

and echoing the results with the hotels, including price, description, name, address, etc.

echo <<<EOF

If anyone around can help me or show me how to write the xml values into the example I think I am done!

Flavio
  • 31
  • 5
  • Does $url ="http://www.somedomain.com/.......&sort=desc"; return XML? What does the &sort=desc in your url do? Doesn't that sort your results for you already? From what you've written I take it you are returned a XML file with hotels already sorted. – Martin Jul 23 '10 at 16:30
  • The "root /item /price.... part" this is XML, why would you want to write that? Isn't this what you are returned in the the variable $url? – Martin Jul 23 '10 at 16:34
  • Nope. Already tried that one =( / Well, is an xml engine, not a searc engine =( – Flavio Jul 23 '10 at 16:34
  • I only need one of the labels, the one which is "minCostOfStay", but I have got no clue on how to do it, already tried $hotel->minCostOfStay and it doesn't seem to work in that context – Flavio Jul 23 '10 at 16:36
  • Maybe I am not explaining me self good. How do I extract that variable and put it on that "manual" written part? – Flavio Jul 23 '10 at 16:37
  • Do you have the values e.g. Price and you want to create a XML file of these so you can pass this to the SimpleXMLElement class (XML engine)? – Martin Jul 23 '10 at 16:37
  • What is the "manual" written part? I have no idea what you are asking. – Martin Jul 23 '10 at 16:41
  • Have a look at an actual example: http://www.lisboando.com/prueba.php?ref=1278890&lg=es&country=portugal&city=lisboa&checkin=2010/07/28&checkout=2010/07/29&rval=1&pval=1 I already got the infom is automatic, it gets it dependant on what you do at the form on the left -days of stay, nr. of persons, etc.-. Now I need to sort the results by price sort or desc – Flavio Jul 23 '10 at 16:41
  • OK, then what information does the form return? Is it XML or does it query a database to get the information? – Martin Jul 23 '10 at 16:44
  • Not sure. It comes from an external source, which is: http://www.somedomain.com/cgi/xml/engine/get_data.php – Flavio Jul 23 '10 at 16:47

1 Answers1

1

Your question is not very clear.

I assume that you are returned an XML file from your url. You then traverse it and fetch the elements out of it.

I suggest you have a look at this post, which may help you solve your question.

Alternatively you may want to fetch each XML value and store these in a php stdClass, then you can sort that object according to any field.

Community
  • 1
  • 1
Martin
  • 10,294
  • 11
  • 63
  • 83