Forum Moderators: coopster

Message Too Old, No Replies

Search function question...

         

AlexLee

8:40 am on Feb 17, 2005 (gmt 0)

10+ Year Member



I have a working seach function here.
And I use teh explode and implode function to seperate individual words.

if(isset($_POST["keyword"]))
{
$keyTemp = $_POST["keyword"];
if($keyTemp == null)
{
echo "You need to enter something in order to search.";
$keywords = "";
}
else
{
$string_array = explode(" ", $keyTemp);
foreach($string_array as $num => $word)
{
$new[$num] = '%' . $word . '%';
}
$keywords = implode(" ",$new);
}
}

My questions is that. How do I solve the problem of people being about to search for "wireless card" and returns results with "wireless" and "card" inside. But not "card wireless"?

I know this is because of the oder of the words appearing in the SQL query. But how do I solve this?

mcibor

9:57 am on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What query do you use? LIKE %search string%? or WHERE bla IN (search string)?

best regards
Michal Cibor

AlexLee

10:44 am on Feb 17, 2005 (gmt 0)

10+ Year Member



LIKE $word1% %word2%

mcibor

1:38 pm on Feb 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



have you tried putting:

LIKE %$word1% $word2%?

I didn't try it out, but I know that % is like * in file search.

Hope it helps
Michal CIbor

AlexLee

10:53 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



opps, actually mine should read: LIKE %word1%%word2%

AlexLee

1:42 pm on Feb 18, 2005 (gmt 0)

10+ Year Member



SELECT * FROM productcatalog_product
WHERE product_manufacturer LIKE '%corp%' and product_manufacturer like '%amd%'

How do I make that instead of doing the above code, I am still about to search for "%corp% %amd%" while the data in the database is "amd query".

Because I can't get the data "amd corp" when I search for "%corp%%amd%" but only "%amd%%corp% works...

mcibor

10:51 pm on Feb 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use:

SELECT * FROM productcatalog_product
WHERE product_manufacturer REGEXP 'corp¦amd';

However this query will find all records containing at least one word (amd or corp) and is case sensitive.

I couldn't find anything else. I think, that using WHERE product LIKE '%corp%' AND product LIKE '%amd%'; would realy give best results.

Best regards
Michal Cibor

hakre

7:52 am on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



LIKE logical combinations (AND/OR) should do the trick. i made this on a big content page and works like a charm.

AlexLee

12:16 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



mcibor: the reason why i don't use WHERE product LIKE '%corp%' AND product LIKE '%amd%'; is because I am doing paging also... If I did that... My whole code would be a big mess...

AlexLee

12:20 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



hakre: can you paste an example of how you managed to solve the problem? Had been trying whole day long with no result...

SELECT * FROM productcatalog_product
WHERE product_manufacturer LIKE '%corp%'
and product_manufacturer LIKE '%amd%'

Any idea on making the above code work without repeating the "and product_manufacturer like" part...

Where the word amd and corp can be interchangable and code still works... This is for a search engine for my site...

mcibor

7:29 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't understand your problem... If you're paginating the result there's no difference between using
product LIKE %here sth%
and
product LIKE %here sth% AND product LIKE %here sth%

Especially that's true if you use built in function for paginating: LIMIT. Then your query would look like:

SELECT * FROM productcatalog_product
WHERE product_manufacturer LIKE '%corp%'
and product_manufacturer LIKE '%amd%' LIMIT 20 OFFSET 0

20 is the number of rows shown and 0 is the starting result row.

Hope this solves your problem. I couldn't find in the sql manual anything more about LIKE function.

Best regards!
Michal Cibor