Search - Whole Words

5 posts by 3 authors in: Forums > CMS Builder
Last Post: January 4, 2011   (RSS)

By newB - January 4, 2011

Hi,
I found a post in another interactivetools forum about Searching only whole words (It's from 2004 - http://www.interactivetools.com/iforum/Products_C2/CMS_Builder_F35/gforum.cgi?post=31614).

Right now, i have a search form on my CMS Builder and when client searches for Ice..the word Lice comes up. Is there an easy fix for this? I tried adding <input type="hidden" name="whole_word" value="2"> to the search form but it didnt seem to work.

Re: [newB] Search - Whole Words

By Jason - January 4, 2011

Hi,

There isn't a way to have CMS Builder do this automatically, however, we can do this be creating our own where clause.

In this example, we'll search a section called news, searching on the title field. The name of the field in our form will be title. Here is how we'll select the records:

$where = "title REGEXP '[[:<:]]". mysql_real_escape_string( @$_REQUEST['title'] ) ."[[:>:]]' = 1 ";

list($newsRecords,$newsMetaData)=getRecords(array(
'tableName' => 'news',
'where' => $where,
'allowSearch' => false,
));


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [newB] Search - Whole Words

By Chris - January 4, 2011

Hi newB,

First, let's change your text field to have a simpler name:

<input name="keyword" type="text" id="textfield" size="17">

Next, we can set up a foreach to loop over the fields you want to search to build up a where clause:

<?php
$escaped_keyword = mysql_escape( @$_REQUEST['keyword'] );
$fields = array('title','content_keyword','subtitle_keyword','category','author','summary_keyword','contents_keyword','about_the_author_keyword','category_keyword');
$where = '';
foreach ($fields as $field) {
if ($where) { $where .= ' OR '; }
$where .= "`" . $field . "` REGEXP '[[:<:]]" . $escaped_keyword . "[[:>:]]' = 1";
}

list($booksRecords, $booksMetaData) = getRecords(array(
'tableName' => 'books',
'where' => $where,
'allowSearch' => false,
);
?>


Does that work for you? Please let me know if you have any questions.
All the best,
Chris

Re: [newB] Search - Whole Words

By newB - January 4, 2011

Chris, that worked out very well! Thank you!