close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Search 'phrase' through Managed Relational Table

Thread began 4/19/2013 11:32 am by paul386190 | Last modified 5/07/2013 11:31 am by paul386190 | 1283 views | 19 replies |

paul386190

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

Sign in to reply to this post

Jason ByrnesWebAssist

The Keyword search type should be set to includes.

For the key word and, enter a space. for the keyword or enter a comma

for the start encapsulator and end encapsulator enter double quotes.

Sign in to reply to this post

paul386190

Hi Jason, many thanks for your response. Unfortunately that doesn't look to have resolved the issue. Please find below the new search code I'm using...


<?php
//WA Database Search (Copyright 2005, WebAssist.com)
//Recordset: keywords;
//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",",","%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 = '';
?>


I've also placed "" around the value itself so that it reads..."<?php echo ((isset($_POST["search-keywords"]))?$_POST["search-keywords"]:""); ?>"... but that hasn't worked.

Is there anything else I've missed?
Many thanks in advance.
Paul

Sign in to reply to this post

Jason ByrnesWebAssist

I'll need to troubleshoot directly, see the private message section.

Note:
It's more helpful to have the full page in a zip file then to pate code segments in your message.

Sign in to reply to this post

paul386190

Hi Jason, I've sent you a private message.

Sign in to reply to this post

Jason ByrnesWebAssist

The problem is that you are searching on the keywords_name column, this column does not contain any records that have the string "iphone 5"

to troubleshoot this, i edited the mouse over text so it would show both the product name and keyword column:

title="<?php echo $row_rsproducts['product_name']; ?>||<?php echo $row_rsproducts['keywords_name']; ?>"

for all of the records where product name includes "iphone 5" the keyword only contains "iphone"

you're trying to search for a string that does not exist in the keywords_name column

Sign in to reply to this post

paul386190

Hi Jason, many thanks for looking into it for me. But I was under the impression that I could do a search for all products that have more than one keyword associated to them, in this instance 'iphone' and '5'. Is this not the case?

Kind regards
Paul

Sign in to reply to this post

Jason ByrnesWebAssist

By forcing the quotes in front of the string, you are forcing the search to look for the literal string "iphone 5"

if you want to search for iphone OR 5, then leave the quotes off the front of the string, and set the OR separator to a space

Sign in to reply to this post

paul386190

Hi Jason, I seem to be back to square one again then mate. I don't want to return any results that have either 'iphone' or '5' assigned to them, I want to return all results that have both 'iphone' and '5' as separately assigned keywords. I've removed the forced quotes and used a space for AND and set the comparison as INCLUDES, but it still gives me no results. It would be the same as if they wanted to search 'black phone', I'd want to show all results that have both 'black' and 'phone' assigned to them. Really sorry if this is getting frustrating for you, it's pulling my hear out trying to establish the best way to get all the products set up for searching. Having read through loads of other posts, it seemed to be best to separate out all the search terms into a separate table and then use a managed relational table to join them to the products rather than having a 'keywords' field within the 'products' table and for each product filling it with all the relevant keywords.

Yours hopefully.
Paul

Sign in to reply to this post

Jason ByrnesWebAssist

i dont follow. I'll need to do a screen sharing session

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...