Search Function. NEED HELP!

[php]<?php

$resultsxml = xml_read(‘projecten.xml’);
$resultsxml = $resultsxml[‘result’];

$getLocatie = $_POST[‘location’];
$getFase = $_POST[‘fase’];
$getBor = $_POST[‘buyorrent’];

$last_results = array();

if(isset($getLocation)){

$last_results = resultFilter($getLocation,$resultsxml);

}else{$last_results = $resultsxml;}

if(isset($getFase)){

$last_results = resultFilter($getFase,$last_results);

}else{

if(empty($getLocatie)){
	
	$last_results = $resultsxml;
}

}

if(isset($getBor)){

if($getBor != "both"){
	
	if($getBor== "buy"){
		
		$fase = "For Sell";
		
	}else if($getBor == "rent"){
		
		$fase = "For Rent";
		
	}
	
	$last_results = filterProjects($last_results, $fase);
}

}
[/php]

This code is used for a real estate website.

A house can have 3 stats the ‘buyorrent’: Rent, Sell, Both.

When you give the command to search for: rent it works fine.
when you give the command to search for: sell it works fine.
when you give the command to search for: both it works fine.

The problem is, that when you search for: rent, it wont display a real estate project that is marked “both” meaning rent and sell.
The same happends when you search for: buy, it wont display a real estate project that is marked “both” meaning rent and sell.

i aint no php-magic kid. i tried tough. red several tutorials about php but it wouldnt work. i think i figured out that the problem lies in this piece of code.
[php]if(isset($getBor)){

if($getBor != "both"){
	
	if($getBor== "buy"){
		
		$fase = "For Sell";
		
	}else if($getBor == "rent"){
		
		$fase = "For Rent";
		
	}[/php]

Can anyone help me fix the code? So that when a command to search for rent or sell, it will display projects marked as “both” meaning rent and sell.

Thanks in advance!

All the logic of search criteria is within this function filterProjects() and it is impossible to give advice without beeing able to see the code of this function.

I’am sorry, i aint that wonderful in php!

But here it is.

[php]<?php

$resultsxml = xml_read(‘projecten.xml’);
$resultsxml = $resultsxml[‘result’];

$getLocatie = $_POST[‘location’];
$getFase = $_POST[‘fase’];
$getBor = $_POST[‘buyorrent’];

$last_results = array();

if(isset($getLocation)){

$last_results = resultFilter($getLocation,$resultsxml);

}else{$last_results = $resultsxml;}

if(isset($getFase)){

$last_results = resultFilter($getFase,$last_results);

}else{

if(empty($getLocation)){
	
	$last_results = $resultsxml;
}

}

if(isset($getBor)){

if($getBor != "both"){
	
	if($getBor== "buy"){
		
		$fase = "For Sell";
		
	}else if($getBor == "rent"){
		
		$fase = "For Rent";
		
	}
	
	$last_results = filterProjects($last_results, $fase);
}

}

function filterProjects($var, $query){

$results = array();

foreach($var as $i => $val){
	$key = in_array($query, $var[$i]);
	if($key == 1){
		array_push($results, $val);
	}
}
return($results);

}

function resultFilter($post,$xml){
$arr = array();
for($i=0;$i<count($post);$i++){

	$query = $post[$i];
	$results = filterProjects($xml, $query);
	
	
	for($z=0;$z<count($results);$z++){
		array_push($arr, $results[$z]);
	}	
}

return($arr);

}

function xml_read($xml) {
if (!$data = implode(’’, file($xml))) {
die(‘kan xml document niet vinden!’);
}
$parser = xml_parser_create();
$params = array();
$level = array();

xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $data, $values, $index);
xml_parser_free($parser);

foreach ($values as $element) {    
    switch($element['type']) {
        case 'open' :
            if (array_key_exists('attributes', $element)) {
                list($level[$element['level']], $extra) = array_values($element['attributes']);
            }
            else { $level[$element['level']] = $element['tag']; }
            break;
            
        case 'complete' :
            $start = 1;
               $exec = '$params';
               while($start < $element['level']) {
                     $exec .= '[$level['.$start.']]';
                     $start++;
               }
               $exec .= '[$element[\'tag\']] = $element[\'value\'];';            
               eval($exec);
            break;
    }
}
return $params;

}
?>[/php]

so this thing is there are 3 search options.

In this state it outputs when searched upon

Buy: listings marked sell
Rent: listings marked rent
Both: listings marked rent and sell

The situation should be:

Buy: listings marked rent AND listings marked both
Sell: listings marked sell AND listings marked both
Both: listings marked sall. listings marked rent. listings marked both.

the php referes to an xml looking like this.

<result> <project id="0"> <titel>Project1</titel> <picture>path/thumb.jpg</pictre> <buyorrent>For Rent</buyorrent> <href>path</href> <status>ontwerp</status> <provincie>Noord-Holland</provincie> </project> <project id="1"> <titel>Project2</titel> <picture>path/thumb.jpg</pictre> <buyorrent>For Sell</buyorrent> <href>path</href> <status>ontwerp</status> <provincie>Zuid-Holland</provincie> </project> <project id="2"> <titel>Project3</titel> <picture>path/thumb.jpg</pictre> <buyorrent>Both</buyorrent> <href>path</href> <status>ontwerp</status> <provincie>Noord-Holland</provincie> </project> </results

still half a sleep !

above post the search options should be!

so this thing is there are 3 search options.

In this state it outputs when searched upon

Buy: listings marked “For Sell”
Rent: listings marked “For Rent”
Both: listings marked “For Rent” and “For Sell”

The situation should be:

Buy: listings marked “For Sell” AND listings marked “Both”
Rent: listings marked “For Rent” AND listings marked “Both”
Both: listings marked “For Sell”. listings marked “For Rent”. listings marked “Both”.

Excuse me !

You need to modify your filterProjects() function:
[php]
function filterProjects($var, $query){
$results = array();
foreach($var as $i => $val){
$extra = false;
if($query == ‘For Sell’ or $query == ‘For Rent’) $extra = in_array(‘Both’, $var[$i]);
$key = ( in_array($query, $var[$i]) or $extra );
if($key){
array_push($results, $val);
}
}
return($results);
}
[/php]

Thank you very much for the help!

I implemented this code.

There still one problem, when i search for rent, it actually shows rent listings, but the listings marked as both wont show up.

The same with buy, it actually shows sell listings, but the listings marked as both wont show up.

Thanks Thanks Thanks a thousand times !! :smiley: :smiley: :smiley: :smiley: :smiley:

You mean, nothing changed comparing to code before these changes?
Do you have this line correct (I edited post after it was submitted - added brackets):
[php]$key = ( in_array($query, $var[$i]) or $extra );[/php]

Nope the output doesn’t change.

when i search on buy: outputs listings for sell
when i search on rent: outputs listings for rent

both searches should also display listings marked as both

Sorry it is hard to understand logic of this code. Functions resultFilter() and filterProjects() are doing almost the same… but they are also calling each other, and their parameters swapped.

Oke Thanks for your help anyway !!

Sponsor our Newsletter | Privacy Policy | Terms of Service