Hello-
I'm writing a search page using Mysql full-text boolean search to search thru data. What I have written works if the user enters the boolean operators, like '+', in the search field (if I dont'use the "qualifier" field in my code below), but I'm trying to make it more user friendly so that the user can select to use all the search terms or any of the search terms rather than having to enter the boolean operators in front of each search term in the search form. But I can't get my code to work. It doesn't return any results. Below is my code for my search form and the page that does the searching. What am I doing wrong? Is there a bettwer way to do this? Any help would be greatly appreciated.

// the search form

<form action="dothesearch.php" method="POST">
<input type="text" name="keywords">


<input type="radio" name="qualifier" value="all">Search using all of your keywords
<input type="radio" name="qualifier" value="any">Search using any of your keywords
<input type="submit" name="submit" value="SEARCH"> <input type="reset" value="Clear form">
//do the search and search using all the terms if 'all'
if ($qualifier == "all"){
$keywords=explode(" ",$keywords);

//add + operator to each keyord
 foreach ($keywords as &$keywords) {
    $keywords = "+".$keywords." ";

 }

}
$query = mysql_query("SELECT *, MATCH(title,companyname2,city2,description) AGAINST('$keywords' IN BOOLEAN MODE) as relevance FROM joblistings WHERE MATCH(title,companyname2,city2,description) AGAINST('$keywords' IN BOOLEAN MODE) and approved='1' and inactive ='0'");

    If you have a post form
    then the values are sent in $_POST array
    So ...

    <?php
    
    // not set?
    if(!isset($_POST['keywords'])) exit('nothing posted');
    
    // get posted values
    $keywords  = $_POST['keywords'];
    $qualifier = $_POST['qualifier'];
    
    //do the search and search using all the terms if 'all'
    if ($qualifier == "all"){
    	$keywords=explode(" ",$keywords);
    
    //add + operator to each keyord
    foreach ($keywords as &$keywords) {
    	$keywords = "+".$keywords." ";
    }
    }
    else{
    	// qualifier='any'
    
    }
    
    $query = mysql_query("SELECT *, MATCH(title,companyname2,city2,description) 
    AGAINST('$keywords' IN BOOLEAN MODE) as relevance FROM joblistings 
    WHERE MATCH(title,companyname2,city2,description) 
    AGAINST('$keywords' IN BOOLEAN MODE) and approved='1' and inactive ='0'"); 
    
    ?>

      Thanks but is still not working. If I enter more than one search term and select 'all', the search only searches the last term entered. For example if I enter two spaced terms: 'microsoft philadelphia', it only searches on philadlephia. Any other ideas?

        I am not 100% sure what exactly you need,
        but here at least you get one string $keywords
        with all words in.

            $keywords .= "+".$word." "; 

        .= means add to, append to string

        <?php
        
        $keywords = 'microsoft philadelphia';
        
        
        $words=explode(" ",$keywords);
        
        //add + operator to each keyord
        $keywords = '';
        foreach($words as $word) {
            $keywords .= "+".$word." ";
        }
        
        echo $keywords;        // '+microsoft +philadelphia '
        $keywords = rtrim($keywords); // remove last space
        echo '<br>'.$keywords; // '+microsoft +philadelphia'
        
        ?>

          It works. Thank you so much.

            Write a Reply...