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: [Jason] Search - Whole Words

By newB - January 4, 2011

Thanks Jason for replying! Not too familiar with mysql and tried your suggestion (by pasting it on the list page), but im getting an error. It think its because i am searching on multiple fields. Right now, this is how my form is...do you have a suggestion on searching multiple fields within that table?

<form name="search" method="post" action="search.php">

<input name="title,content_keyword,subtitle_keyword,category,author,summary_keyword,contents_keyword,about_the_author_keyword,category_keyword" type="text" id="textfield" size="17">

<input name="submit" type="image" value="Search" src="images/images/search_btn.jpg" alt="" align="left" width="37" height="48">
</form>

I am searching multiple fields on a table called "books"
This is what i have on the listviewer.php

list($booksRecords, $booksMetaData) = getRecords(array(
'tableName' => 'books',

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!