SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Excluding certian words from a website search

    hi,

    Could someone take a look at this for me and tell me how I can add a word that if searched for would return NO results? I sell angel figurines and if they put the word angels (which a lot do) it returns everything in the site. Example, they might put "angels with cats" or something like that and they'll get everything because they use the word angels.

    Code:
    // make sure this file isn't accessed directly via a browser
    if (!defined('SC_INCLUDE_OK')) die;
    
    // grab the search array from submitted data
    $search = $_GET['search'];
    
    // set default for mode if it was not passed
    if (!$search['mode']) $search['mode'] == 'any';
    
    // grab the search terms and put 'em in an array
    if (isset($search['terms']) && is_array($search)) {
    	
    	if (strlen($search['terms']) == 0) {
    		$Result_Message = '<div class="action_msg">You must enter search terms to perform a search!</div>';
    	} else {
    
    		// strip slashes if necessary
    		if (get_magic_quotes_gpc()) $search['terms'] = stripslashes($search['terms']);
    	
    		// remove extra whitespace
    		$search['terms'] = trim($search['terms']);
    
    		// if terms entered start and end with a quote, then treat it as an exact match
    		if ($search['terms'][0] == '"' && $search['terms'][strlen($search['terms'])-1] == '"') {
    			$terms[] = str_replace('"','',$search['terms']);
    		} elseif ($search['mode'] == 'exact') {
    			// exact search should just use the whole value of the terms field
    			$terms[] = $search['terms'];
    		} else {
    			// explode words into an array, one element per word and loop through them
    			foreach(explode(' ',$search['terms']) as $term) {
    				// remove white space
    				$term = trim($term);
    				// escape the term to make it safe for the query, and put it in an array
    				if ($term) $terms[] = sc_mysql_escape($term);
    			}
    		}
    		
    	}
    
    }
    
    
    
    /********************************************************************
    	Setup a MySQL query for search
    ********************************************************************/
    
    if ($_GET['rpt_qry']) {
    	// grab query and search fields from cache....probably just navigating to a diff. page
    	$query 			= $SC['search_cache'];
    	$search 		= $SC['search_flds_cache'];
    	$sort_selected 	= $SC['sort_selected'];
    } elseif(is_array($terms)) {
    	
    	// setup field names that will be searchable
    	// make this configurable later
    	$search_columns = array('Products.Name','Products.Description','Products.Keywords','c.Name');
    
    	// loop through each term
    	foreach($terms as $term) {
    		// loop through each column that will be searched
    		foreach($search_columns as $search_column) {
    			// setup an array, where each element is a piece of the where string
    			$where_pieces[] .= "$search_column LIKE '%$term%'";
    		}
    		
    		// each element in the array below is a string that will search for one term, in all columns
    		// like:
    		// $where_strings[0] -	(Products.Name LIKE '%term1%' OR Products.Description LIKE '%term1%')
    		// $where_strings[1] -	(Products.Name LIKE '%term2%' OR Products.Description LIKE '%term2%')
    		$where_strings[] = '('.implode(' OR ',$where_pieces).')';
    		unset($where_pieces);
    	}
    
    	// setup the where string, depending on the search mode (any, all, exact)
    	// exact does not really change this, as it is only used to effect the extraction
    	// of terms from the search field, so it is treated like "any" as well
    	if ($search['mode'] == 'any' || $search['mode'] == 'exact') {
    		$where_string = implode(' OR ',$where_strings);
    	} elseif ($search['mode'] == 'all') {
    		$where_string = implode(' AND ',$where_strings);
    	}
    Thanks

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try adding an if inside the foreach. First add somewhere an array with words to be excluded:

    PHP Code:
    $excludeWords = array('angels'); 
    And then the if in foreach:
    PHP Code:
    // explode words into an array, one element per word and loop through them
                
    foreach(explode(' ',$search['terms']) as $term) {
                    
    // remove white space
                    
    $term trim($term);
                                    if (
    array_search($term$excludeWords) !== FALSE)
                                    {
                                        continue;
                                    } 

  3. #3
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    I've been working on it most of the day but I couldn't get the code to work. Here's how I have it now. There are no errors, just seems to be no change at all. I've tried many things and combinations with no luck.

    PHP Code:
    // make sure this file isn't accessed directly via a browser
    if (!defined('SC_INCLUDE_OK')) die;

    // grab the search array from submitted data
    $search $_GET['search'];
    $excludeWords = array('angels'); 
    // set default for mode if it was not passed
    if (!$search['mode']) $search['mode'] == 'any';
     
    // grab the search terms and put 'em in an array
    if (($search['terms']) && is_array($search)){
        
        if (
    strlen($search['terms']) == 0){
            
    $Result_Message '<div class="action_msg">You must enter search terms to perform a search!</div>';
        } else {

            
    // strip slashes if necessary
            
    if (get_magic_quotes_gpc()) $search['terms'] = stripslashes($search['terms']);
        
            
    // remove extra whitespace
            
    $search['terms'] = trim($search['terms']);

            
    // if terms entered start and end with a quote, then treat it as an exact match
            
    if ($search['terms'][0] == '"' && $search['terms'][strlen($search['terms'])-1] == '"') {
                
    $terms[] = str_replace('"','',$search['terms']);
            } elseif (
    $search['mode'] == 'exact') {
                
    // exact search should just use the whole value of the terms field
                
    $terms[] = $search['terms'];
                
            } else {
                
    // explode words into an array, one element per word and loop through them
                
    foreach(explode(' ',$search['terms']) as $term) {
                    
    // remove white space
                    
    $term trim($term);
                    if (
    array_search($term$excludeWords) !== FALSE
                                    {   
                                      
    $Result_Message '<div class="action_msg">Hello!</div>';
                                     } 
                    
    // escape the term to make it safe for the query, and put it in an array
                    
    if ($term$terms[] = sc_mysql_escape($term);
                }    
            
            }
            
        }



  4. #4
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One alternative solution would be changing:

    PHP Code:
    if ($term$terms[] = sc_mysql_escape($term); 
    To:

    PHP Code:
    if (!in_array($term$excludeWords)) $terms[] = sc_mysql_escape($term); 
    Using the above mentioned array.

    This should simply exclude the words in the $excludeWords array from the search. So if someone searched for "angels with cats", then it would only search for "with cats".
    Brad Hanson, Web Applications & Scalability Specialist
    ► Is your website outgrowing its current hosting solution?
    ► PM me for a FREE scalability consult!
    ► USA Based: Available by Phone, Skype, AIM, and E-mail.

  5. #5
    SitePoint Addict
    Join Date
    Mar 2008
    Location
    Sterling, CO
    Posts
    275
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    It works beautiful.

  6. #6
    SitePoint Addict
    Join Date
    Jul 2008
    Location
    sudo rm -rf /
    Posts
    386
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you use str_replace($arr,'',$search);

    It's the fastest.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •