PHP Search XML based on price ascending
Here is a sample search: Search Results Page
Does anyone know how I could return that search from my xml based on a cheapest first (or most expensive depending on what the user wants)
Obviously I need another drop down box on my form to the left (Price Ascending/Descending etc) but what sort of code would I need?
This is the a sample of the xml being searched:
<PROPERTY> <REF>fr1113mh</REF> <TYPE>apartment</TYPE> <TERM>rent</TERM> <DURATION>holiday</DURATION> <AREA>Torrox</AREA> <BEDROOMS>1 bedroom</BEDROOMS> <PRICE>From <![CDATA[€]]>215</PRICE> <IMAGE>http://www.for-rent-nerja.com/images/holiday_rental_thumbnails/fr1113mh.jpg</IMAGE> <URL>http://www.for-rent-nerja.com/property.php?REF=fr1113mh</URL> <DATAPROPERTY><![CDATA[ ]]>Open-plan kitchen <![CDATA[<br /> ]]> Living room <![CDATA[<br /> ]]> 1 Bathroom<![CDATA[<br /> ]]> 1 Double Bed <![CDATA[<br /> ]]> 1 Sofa Bed </DATAPROPERTY> <REGION>Torrox Park</REGION> <DATAFEATURE><![CDATA[ ]]>Terrace <![CDATA[<br /> ]]> Hob <![CDATA[<br /> ]]> Oven <![CDATA[<br /> ]]> Refrigerator <![CDATA[<br /> ]]> Washing Machine <![CDATA[<br /><br /> ]]> <![CDATA[<a class="t_color1 next" href="http://www.for-rent-nerja.com/property.php?REF=fr1113mh">View apartment</a>]]></DATAFEATURE> </PROPERTY>
Here is the php that runs the search
<?php
$xml = simplexml_load_file('property_catalog.xml');
$results = array();
$query = '/CATALOG/PROPERTY';
if(isset($_GET['DURATION']) && strlen($_GET['DURATION']) > 0)
$query .= '[DURATION="'.$_GET['DURATION'].'"]';
if(isset($_GET['TYPE']) && strlen($_GET['TYPE']) > 0)
$query .= '[TYPE="'.$_GET['TYPE'].'"]';
if(isset($_GET['AREA']) && strlen($_GET['AREA']) > 0)
$query .= '[AREA="'.$_GET['AREA'].'"]';
if(isset($_GET['REGION']) && strlen($_GET['REGION']) > 0)
$query .= '[REGION="'.$_GET['REGION'].'"]';
if(isset($_GET['BEDROOMS']) && strlen($_GET['BEDROOMS']) > 0)
$query .= '[BEDROOMS="'.$_GET['BEDROOMS'].'"]';
$results = $xml->xpath($query);
echo '<h1>Results ('.((is_array($results) && count($results) > 0) ? count($results) : 0).')</h1>';
if(is_array($results) && count($results) > 0)
{
//This is the number of results to display per page
$limit=4;
//Here we figure out how many pages there are going to be
//Size is the size of the array
$size=count($results);
//Pages is calculated by dividing size by the limit per page
//We use ceil in case it does not divide evenly
$pages=ceil($size/$limit);
$search="";
if(isset($_GET['DURATION']) && strlen($_GET['DURATION']) > 0)
$search .= "&DURATION=" . $_GET['DURATION'];
if(isset($_GET['TYPE']) && strlen($_GET['TYPE']) > 0)
$search .= "&TYPE=" . $_GET['TYPE'];
if(isset($_GET['AREA']) && strlen($_GET['AREA']) > 0)
$search .= "&AREA=" . $_GET['AREA'];
if(isset($_GET['REGION']) && strlen($_GET['REGION']) > 0)
$search .= "®ION=" . $_GET['REGION'];
if(isset($_GET['BEDROOMS']) && strlen($_GET['BEDROOMS']) > 0)
$search .= "&BEDROOMS=" . $_GET['BEDROOMS'];
if(isset($_GET['page']))
$i=$_GET['page'];
else
$i=1;
if(isset($_GET['page']))
$page=$_GET['page'];
else
$page=1;
$start=($page*$limit)-$limit;
$display=array();
while($limit!=0)
{
if($start<$size)
$display[]=$results[$start];
$start++;
$limit--;
}
foreach($display as $property)
{
echo '<div class="price t_color1" > ' . $property->PRICE .' </div> <div class = "t_color2"> Property Ref Number: '.$property->REF .'</div><div class = "t_color1 t_bold"> '. $property->BEDROOMS.$property->PLUS .' ' .$property->TYPE.' in '.$property->AREA .'</div> <div class = "content1_img float_l">
<a href ="'. $property->URL .'"><img src="'. $property->IMAGE .'"></a> </div> <div class = "content1_text float_l"> <b class="t_color5">Property</b><br /><br /> '. $property->DATAPROPERTY . '<br /> <br /> Area: ' . $property->REGION .'</div> <div class = "content2_text float_2"><b class="t_color5">Features</b><br /> <br /> '. $property->DATAFEATURE . '</div> <div class = "left_col float_l"><p><img src = "http://www.for-rent-nerja.com/images/spacer2.jpg" alt = "" width="500"/></p></div>' ;
}
echo "<div align=\"right\">";
if($i!=1)
echo "<a href=\"?page=" . ($i-1) . "$search\" class=\"t_color1\">Previous Page</a> ";
echo "Page " . $i . " of " . $pages . " ";
if($i!=$pages)
echo "<a href=\"?page=" . ($i+1) . "$search\" class=\"t_color1\">Next Page</a> ";
echo "</div>";
}
else
echo '<h1>No Results..</h1> Please broaden your search';
?>