More Search Function Questions

14 posts by 7 authors in: Forums > CMS Builder
Last Post: October 18, 2010   (RSS)

Re: [zickey] More Search Function Questions

By Chris - September 12, 2010 - edited: September 12, 2010

Hi zickey,

Can you make sure your field list ends with "_keyword"?

For example, the last field in this list is "contact_name":

<input type="text" name="region,primary_community,location,start,description,sponsor,contact_name_keyword" ...

The correct format is:

FIELD,FIELD,FIELD,...,FIELD_keyword

If that doesn't help, please post your <FORM> code and your getRecords() code.
All the best,
Chris

Re: [chris] More Search Function Questions

By Mikey - October 15, 2010

Hey Chris,
I'm still not having any luck getting my searchEvents.php page to search the event records case insensitive. I've pasted the code for my page below. Is there something I'm doing wrong that is causing the search to only produce result based on case sensitivity?

Thanks for the help,
Zick

====== begin code =======
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php

require_once "/var/www/hosts/domain.com/httpdocs/cms/lib/viewer_functions.php";

// begin code for search specific page (searchEvents.php) content
list($searchRecords, $searchMetaData) = getRecords(array(
'tableName' => 'search',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$searchRecord = @$searchRecords[0]; // get first record

// show error message if no matching record is found
// if (!$searchRecord) {
// header("HTTP/1.0 404 Not Found");
// print "Record not found!";
// exit;
//}

?>
<?php
// search viewer code to search event records begins
$searchOptions = array();
$searchOptions['keywords'] = @$FORM['q'];
$searchOptions['perPage'] = "10";
$searchOptions['debugSql'] = "0";

$searchTables = array();

$searchTables['events'] = array(
'viewerUrl' => 'eventDetails.php',
'titleField' => 'title',
'summaryField' => 'content',
'searchFields' => array('title','date','content','description','category'),
);

list($searchRows, $searchDetails) = searchMultipleTables($searchTables, $searchOptions);
// search viewer code ends
?>
<?PHP
function maxWords($textOrHtml, $maxWords) {
$text=str_replace("<p>","*P*",$textOrHtml);
$text= str_replace("</p>","*/P*",$text);
$text = strip_tags($text);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="...</p>";

return $output;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>

</head>
<body>

<h1>Search Events</h1>

<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="text" name="q" value="<?php echo htmlspecialchars(@$FORM['q']); ?>" size="50" />
<input type="submit" name="input" value="Search" /><input name="reset" type="button" onclick="parent.location='searchEvents.php'" value="reset" />
<br/>
<br/>
<table border="0" cellpadding="0" cellspacing="0" width="98%">
<tr>
<td><h3> Search results for <b><?php echo htmlspecialchars(@$FORM['q']); ?></b></h3></td>
<td align="right"><p>Results <b><?php echo htmlspecialchars(@$searchDetails['pageResultsStart']); ?></b> to <b><?php echo htmlspecialchars(@$searchDetails['pageResultsEnd']); ?></b> of <b><?php echo htmlspecialchars(@$searchDetails['totalRecords']); ?></b>.</p></td>
</tr>
</table>
<!-- show errors -->
<p><?php if ($searchDetails['invalidPageNum']): ?>
Results page '<?php echo $searchDetails['page']?>' not found, <a href="<?php echo $searchDetails['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
<?php elseif ($searchOptions['keywords'] && $searchDetails['noRecordsFound']): ?>
No records matched search query!<br/>
<?php elseif ($searchOptions['keywords'] == ""): ?> </p>
<?php endif ?>

<!-- display results -->
<?php foreach ($searchRows as $record): ?></p>
<h2><a href="<?php echo $record['_link'] ?>"><?php echo $record['_title'] ?></a></h2>
<p><?php if ($record['_summary']): ?>
<?php echo maxWords($record['_summary'], 40); ?></p>
<?php else: ?>
<p>No description available for page.</p>
<?php endif ?>
<h5><a href="<?php echo $record['_link'] ?>"><strong>Read more</strong></a></h5>
<?php endforeach ?>
<!-- /display result -->

<!-- display links -->
<h5><strong>
<?php if ($searchDetails['prevPage']): ?>
<a href="<?php echo $searchDetails['prevPageLink'] ?>">prev</a>
<?php else: ?>
prev
<?php endif ?>
- page <?php echo $searchDetails['page'] ?> of <?php echo $searchDetails['totalPages'] ?> -
<?php if ($searchDetails['nextPage']): ?>
<a href="<?php echo $searchDetails['nextPageLink'] ?>">next </a>
<?php else: ?>
next
<?php endif ?>
</strong></h5>
<!-- /display links -->
</form>

</body>
</html>

====== /end code ========

Hi zickey,

Can you make sure your field list ends with "_keyword"?

For example, the last field in this list is "contact_name":

<input type="text" name="region,primary_community,location,start,description,sponsor,contact_name_keyword" ...

The correct format is:

FIELD,FIELD,FIELD,...,FIELD_keyword

If that doesn't help, please post your <FORM> code and your getRecords() code.

Re: [zick] More Search Function Questions

By Jason - October 18, 2010

Hi Zickey,

Since you're only searching against 1 table, you won't need to use the searchMultipleTables() function. You could try searching like this:

list($resultRecords,$resultMetaData) = getRecords(array(
'tableName' => 'events',
'allowSearch' => false,
'perPage' => 10,
'where' => "LOWER(title) LIKE '%".mysql_escape(strtolower(@$_REQUEST['q']))."%' OR LOWER(date) LIKE '%".mysql_escape(strtolower(@$_REQUEST['q']))."%' OR LOWER(content) LIKE '%".mysql_escape(strtolower(@$_REQUEST['q']))."%' OR LOWER(description) LIKE '%".mysql_escape(strtolower(@$_REQUEST['q']))."%' OR LOWER(category) LIKE '%".mysql_escape(strtolower(@$_REQUEST['q']))."%'",
));


This code will convert everything to lowercase before doing the search.

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/