Combine two fields for search engine results
3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 10, 2013 (RSS)
By Mikey - January 8, 2013 - edited: January 8, 2013
I need my search engine to search the combined fields (prefix_number,num), so if someone enters XYZ12 into the search engine, it produces the record results associated with the prefix_number,num. However if I search for XYZ12 a combination of the (prefix_number,num) fields it produces no results, but if I search for XYZ only or 12 only my record shows up in the search results. Does anyone have any suggestions how I can search for both (XYZ12) prefix_number,num combined using the search tool and get my search engine to produce the desired results for the record?
Item Number: XYZ12
<?php echo $record['prefix_number'] ?> = XYZ
<?php echo $record['num'] ?> = 12
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<input name="title,content,prefix_number,num,summary_keyword" type="text" value="" size="40" />
<input name="submit" type="submit" value="Search"/>
</form>
<h4><strong>Item Number: <?php echo $record['prefix_number'] ?><?php echo $record['num'] ?> <?php echo $record['title'] ?></strong></a></h4>
Thanks Zick
By Mikey - January 9, 2013
Anyone have some suggestions on the topic of joining two fields for the search engine?
By gregThomas - January 10, 2013 - edited: January 10, 2013
Hi Zack,
Sorry for the delay in reply.
I think the easiest solution would be to have another field in your section that stores the full item number code, and then just include this in the search term. You could create either a cron job, or if you hav experience of plugins you could create one to combine items num value and product letters into the field, or get the user to manually add them when they create the each record.
You could also try creating some code to detect when a user has entered a word that is five letters long (assuming the product number is always that long), gets any numbers from the word and then carry out a search for a record with that num value (as the num value will always be unique). If you used this method, you wouldn't be able to use the default search functionality built into CMS Builder, and would have to customise it slightly. I've done some local testing, and this method seems to work:
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('W:/g/greg.com/htdocs/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load records from 'blog'
list($results, $resultMeta) = getRecords(array(
'tableName' => 'sectionName',
'loadUploads' => false,
'allowSearch' => true,
));
//Get the search term, and explode the string into an array of words
$stringArray = explode(' ', $_REQUEST['title,content,prefix_number,num,summary_keyword']);
//This array contains all of the searched items
$intArray = array();
foreach($stringArray as $word){
//If the word is 5 letters long
if(strlen($word) == 5){
//filter out any letters in contains and store it in the int array
$intArray[] = filter_var($word, FILTER_SANITIZE_NUMBER_INT);
}
}
foreach($intArray as $int){
//check if there are any entries in the section with that int num value
$tempItem = mysql_get('sectionName',$int);
if($tempItem){
$results[] = $tempItem;
}
}
You would need to modify the code so that it uses the correct secion names, and search term.
Thanks!
Greg
PHP Programmer - interactivetools.com