Search - Whole Words

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

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: [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!