Searching for multiple keywords in multiple fields

14 posts by 4 authors in: Forums > CMS Builder
Last Post: December 1, 2009   (RSS)

I see...

How "custom" are we talking?

J
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Searching for multiple keywords in multiple fields

By Dave - December 1, 2009

For your existing search query you'd need to set 'allowSearch' => false, and then create a where query that contains the fields and keywords and looked like this:

'start_date' LIKE '%neubert%' OR 'title' LIKE '%neubert%' OR
'content' LIKE '%neubert%' OR 'list_page_description' LIKE '%neubert%' OR
'start_date' LIKE '%morgan%' OR 'title' LIKE '%morgan%'
OR 'content' LIKE '%morgan%' OR 'list_page_description' LIKE '%morgan%'

If you want to see what it would be like you could try changing this line in /cmsAdmin/lib/viewer_functions.php from:
$where = join(" AND ", $conditions);

to:
$where = join(" OR ", $conditions);

But I really don't think it's what you want. As even your search text says "Enter as many keywords as you like to narrow your search" and you'll actually be expanding your search.

You can put OR between words in google to see the difference:
http://www.google.com/search?q=content+management+system
http://www.google.com/search?q=content+OR+management+OR+system

But if you really want to do it this code should serve as a starting point:

// create OR where using keywords in 'keywords' form field
$fields = array('title','content');
$keywords = preg_split('/\s+/', @$_REQUEST['keywords']);
$where = '';
foreach ($keywords as $keyword) {
if ($keyword == '') { continue; }
foreach ($fields as $field) {
if ($where) { $where .= ' OR '; }
$where .= "'$field' LIKE '%" .mysql_real_escape_string($keyword). "%'";
}
}


A search for "one two three" like this: ?keywords=one%20two%20three
produces:
'title' LIKE '%one%' OR 'content' LIKE '%one%' OR 'title' LIKE '%two%' OR 'content' LIKE '%two%' OR 'title' LIKE '%three%' OR 'content' LIKE '%three%'

Hope that helps! :)
Dave Edis - Senior Developer
interactivetools.com

By gkornbluth - December 1, 2009 - edited: December 1, 2009

Dave,

Thanks for this. I'll play around with it and possibly rethink my approach.

Best,

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php