I have a simple search box that looks in two database fields for search terms. One field contains an item number and the second one keywords. The keywords are separated by comma and space, and can also be keyword phrases.
Search form HTML:
Search Our Site:
<form method="get" action="[[~25]]">
<input type="text" name="search" value="" />
<input type="submit" value="Go" />
</form>
In the snippet:
if (isset($_GET['search']) && (! empty($_GET['search']))) {
$term = $_GET['search'];
} else {
$term = "";
}
and
$c = $modx->newQuery('Products');
$c->where(array('num:LIKE' => '%'.$term.'%'));
$c->where(array('keyw:LIKE' => '%'.$term.'%'),xPDOQuery::SQL_OR);
This works great for the item numbers, you can find complete numbers or with the help of % as a wildcard you can search with only a part of an item number.
For the keywords it gets tricky. You can search for a single keyword just fine, but if you're searching with two it only works when you separate them with the wildcard %. Plus it only works when you enter the keywords in the order they're in in the keyword field.
For example, the content of the keywords field is: apple, orange, grape, melon, green, red, orange, yellow
When you search: apple%yellow it finds all yellow apples. But when you search yellow%apple, it gives no result.
I would like this keyword part to work better, any help is very much appreciated!