PHP Search XML based on price ascending

0 replies
Hey guys.

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:

Code:
<PROPERTY>
    <REF>fr1113mh</REF>
    <TYPE>apartment</TYPE>
    <TERM>rent</TERM>
    <DURATION>holiday</DURATION>
    <AREA>Torrox</AREA>
    <BEDROOMS>1 bedroom</BEDROOMS>
    <PRICE>From <![CDATA[&euro;]]>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[&nbsp;&nbsp;]]>Open-plan kitchen <![CDATA[<br />&nbsp;]]> Living room <![CDATA[<br />&nbsp;]]> 1 Bathroom<![CDATA[<br />&nbsp;]]> 1 Double Bed <![CDATA[<br />&nbsp;]]> 1 Sofa Bed </DATAPROPERTY> 
    <REGION>Torrox Park</REGION>     
    <DATAFEATURE><![CDATA[&nbsp;&nbsp;]]>Terrace <![CDATA[<br />&nbsp;]]> Hob <![CDATA[<br />&nbsp;]]> Oven <![CDATA[<br />&nbsp;]]> Refrigerator <![CDATA[<br />&nbsp;]]> Washing Machine <![CDATA[<br /><br />&nbsp;]]> <![CDATA[<a class="t_color1 next" href="http://www.for-rent-nerja.com/property.php?REF=fr1113mh">View apartment</a>]]></DATAFEATURE>
    </PROPERTY>
So the items I need to sort is the <PRICE>From <![CDATA[&euro;]]>215</PRICE> fields. I'm thinking it will be easier to echo out the 'From <![CDATA[&euro;]]>' in the php to just leave digits but then not sure where to go from there. :confused:

Here is the php that runs the search

PHP Code:
<?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 .= "&REGION=" $_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 /> &nbsp; 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';
        
?>
Any advice/pointing in the right direction would be most welcomed! Cheers guys!
#based #php #search #xml

Trending Topics