The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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);
}

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.

// 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);
}