Search 'phrase' through Managed Relational Table
Hi there, I wonder if anyone can assist. I'm creating a free text search (a single text-input named 'search-keywords') on a website for narrowing down a huge range of products, using the DataAssist Search feature.
The database has three tables:
1. Products: includes product_id, product_name, other product details
2. Keywords: incorporates keywords_id and keywords_name
3. KeywordProduct: incorporates KeywordProduct.KeywordProductID, KeywordProduct.KeywordProductProductID and KeywordProduct.KeywordProductKeywordID
Table 3 is a managed relational table so that a product can be assigned multiple keywords.
The products we have on the site include iPhone 3, iPhone 4, iPhone 5, iPad 2 and iPad 3 so we've opted to use keywords in the style of 'iPhone', 'iPad', '3', '4', '5', rather than complete model numbers as it will be easier when future models are brought out.
The struggle I'm having with the search is when more than one word is entered into the field. So if I search 'iPhone' I get all the products which have 'iPhone' assigned to them and if I search '5' I get all the products which have '5' assigned to them, but if I search 'iPhone 5' I get no results. What we need to happen in this instance is to get all products that have BOTH keywords assigned to them, not just either of them. I've tried a comination of 'include', '=', space for KEYWORD AND, and adding "" around the search term, but to no avail.
This is the code I currently have for the search...
<?php
//WA Database Search (Copyright 2005, WebAssist.com)
//Recordset: rsproducts;
//Searchpage: ;
//Form: ;
$WADbSearch1_DefaultWhere = "";
if (!session_id()) session_start();
if (isset($_POST["search_submit"]) || isset($_POST["search_submit_x"])) {
$WADbSearch1 = new FilterDef;
$WADbSearch1->initializeQueryBuilder("MYSQL","1");
//keyword array declarations
$KeyArr1 = array("keywords_name");
//comparison list additions
$WADbSearch1->keywordComparison($KeyArr1,"\"".((isset($_POST["search-keywords"]))?$_POST["search-keywords"]:"") ."\"","AND","Includes","%20","OR","%22","%22",0);
//save the query in a session variable
if (1 == 1) {
$_SESSION["WADbSearch1_masthead"]=$WADbSearch1->whereClause;
}
}
else {
$WADbSearch1 = new FilterDef;
$WADbSearch1->initializeQueryBuilder("MYSQL","1");
//get the filter definition from a session variable
if (1 == 1) {
if (isset($_SESSION["WADbSearch1_masthead"]) && $_SESSION["WADbSearch1_masthead"] != "") {
$WADbSearch1->whereClause = $_SESSION["WADbSearch1_masthead"];
}
else {
$WADbSearch1->whereClause = $WADbSearch1_DefaultWhere;
}
}
else {
$WADbSearch1->whereClause = $WADbSearch1_DefaultWhere;
}
}
$WADbSearch1->whereClause = str_replace("\\''", "''", $WADbSearch1->whereClause);
$WADbSearch1whereClause = '';
?>
And this is the query I'm using for gathering the products....
mysql_select_db($database_lazerbuilt, $lazerbuilt);
$query_rsproducts = "SELECT * FROM product, keywords, KeywordProduct WHERE product.product_id = KeywordProduct.KeywordProductProductID and KeywordProduct.KeywordProductKeywordID = keywords.keywords_id";
setQueryBuilderSource($query_rsproducts,$WADbSearch1,false);
$rsproducts = mysql_query($query_rsproducts, $lazerbuilt) or die(mysql_error());
$row_rsproducts = mysql_fetch_assoc($rsproducts);
$totalRows_rsproducts = mysql_num_rows($rsproducts);
I would be very grateful for any assistance.
Many thanks in advance
Paul