This is quite frustrating have been working on it all day.
I am trying to sort The XML document with price from Lowest to highest.
Using PHP.
The XML data is only using the City London, Just for reference
and i did not wish to place the Price in descending order on the XML due to the information i wish to manipulate will not be added statically.
First the codes
var xHRObject = false;
if (window.XMLHttpRequest)
xHRObject = new XMLHttpRequest();
else if (window.ActiveXObject)
xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
function retrieveInformation()
{
var city = document.getElementById("selectCity").value;
var type = "";
var input = document.getElementsByTagName("input");
for (var i=0; i < input.length; i++)
{
if (input.item(i).checked == true)
type = input.item(i).value;
}
xHRObject.open("GET", "retrieveHotelInfo.php?id=" + Number(new Date) +"&city=" + city + "&type=" + type, true);
xHRObject.onreadystatechange = function() {
if (xHRObject.readyState == 4 && xHRObject.status == 200)
document.getElementById('information').innerHTML = xHRObject.responseText;
}
xHRObject.send();
}
-
<html>
<head>
<script type="text/javascript" src="retrieveHotelInfo.js"></script>
</head>
<body>
Destination:
<br />
<select id="selectCity" onchange="retrieveInformation()">
<option value="London" selected="true">London</option>
<option value="Paris">Paris</option>
<option value="New York">New York</option>
<option value="Chicago">Chicago</option>
<option value="Seattle">Seattle</option>
</select>
<br />
<br />
Price Range:
<br />
Budget<input name="range" value="Budget" type="radio" onclick="retrieveInformation()"/>
Standard<input name="range" value="Standard" type="radio" onclick="retrieveInformation()" checked="true"/>
Luxury<input name="range" value="Luxury" type="radio" onclick="retrieveInformation()"/>
<div id="information">
</div>
</body>
</html>
-
<?php
$xmlFile = "hotel.xml";
$HTML = "";
$count = 0;
$dt = simplexml_load_file($xmlFile);
$dom = DOMDocument::load($xmlFile);
$hotel = $dom->getElementsByTagName("hotel");
foreach($hotel as $node)
{
$city = $node->getElementsByTagName("City");
$city = $city->item(0)->nodeValue;
$type = $node->getElementsByTagName("Type");
$type = $type->item(0)->nodeValue;
$name = $node->getElementsByTagName("Name");
$name = $name->item(0)->nodeValue;
$price = $node->getElementsByTagName("Price");
$price = $price->item(0)->nodeValue;
if (($type == $_GET["type"]) && ($city == $_GET["city"]) )
{
$HTML = $HTML."<br><span>Hotel: ".$name."</span><br><span>Price: ".$price."</span><br>";
$count++;
}
}
if ($count ==0)
{
$HTML ="<br><span>No hotels available</span>";
}
echo $HTML;
?>
if needed the xml document is written just like this
<?xml version="1.0"?>
<hotels>
<hotel>
<City>London</City>
<Name>The Rilton</Name>
<Type>Luxury</Type>
<Price>300</Price>
</hotel>
<hotel>
<City>London</City>
<Name>The Rilton</Name>
<Type>Budget</Type>
<Price>150</Price>
</hotel>
<hotel>
<City>London</City>
<Name>The Rilton</Name>
<Type>Standard</Type>
<Price>250</Price>
</hotel>
<hotel>
<City>London</City>
<Name>The Lolipop</Name>
<Type>Standard</Type>
<Price>280</Price>
</hotel>
<hotel>
<City>London</City>
<Name>The non-Rilton</Name>
<Type>Standard</Type>
<Price>225</Price>
</hotel>
</hotels>