shoeyn Posted August 20, 2012 Share Posted August 20, 2012 I'm trying to make a function that will save each combination of words of a string into an array for a site search. (I understand the amount of searches it will be with a lot of words). Below is the code I have so far... I know that it currently won't do as intended, but I'm currently stuck because there's a memory exhaustion problem. Can anybody help me ? $searchTerm = "1 2 3"; function check($var) { global $j, $search; if ($var != $search[$j]) return true; else return false; } $arrange = array(); $search = explode(" ", $searchTerm); $amount = count($search); $poss = 1; for ($i = 2; $i <= $amount; $i++) { $poss *= $i; } $last = $amount - 1; if ($poss > 1) { $i = 1; $arrange[] = "%" . implode("%", $search) . "%"; while ($i != $poss) { $j = 0; while ($j != $last) { $l = $last; $f = $last - 1; while ($f != 0) { $arrange[] = "%" . $search[$j] . "%" . $search[$l] . "%" . $search[$f] . "%"; $f--; $l--; $i++; } $j++; $arrange[] = "%" . $search[$j] . "%" . implode("%", array_filter($search,"check")) . "%"; $i++; } } } echo implode(",", $arrange); For example if the user typed "1 2 3", I would like the function to produce %1%2%3%, %1%3%2%, %2%1%3%, %2%3%1%, %3%1%2%, %3%2%1% Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 20, 2012 Share Posted August 20, 2012 There is a user submitted function in the php manual that might do what you need: www.php.net/manual/en/function.shuffle.php#90615 I had a similar question recently: http://forums.phpfreaks.com/index.php?topic=363212.msg1718676#msg1718676 Check out Barand's answer, and the one in the php manual. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 20, 2012 Share Posted August 20, 2012 I'm trying to make a function that will save each combination of words of a string into an array for a site search. If I am understanding you correctly, then what you really need is to implement FULL TEXT searching instead. What you are trying to do is inefficient and is basically trying to recreate functionality already built into MySQL. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 20, 2012 Share Posted August 20, 2012 I'm currently stuck because there's a memory exhaustion problem. That's not something that can be solved. Looks like search functionality - is the input coming from the user? Best be very careful about what you allow or else I'll enter the quick brown fox jumps over the lazy dog That's 362,880 combinations, and at 45B per string that's 16MB for an absolute minimum amount of memory (which you'll be nowhere near). And I'm not even trying. And that's not even considering the database portion. Quote Link to comment Share on other sites More sharing options...
shoeyn Posted August 20, 2012 Author Share Posted August 20, 2012 It's something a friend asked for, for his back panel use only. He struggles to find product names because he types the words of the product name in the wrong order and they don't appear. Maximum he types is 4 words that's why he doesn't mind using this method. And there's a memory leak in my function, I just don't know where. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 20, 2012 Share Posted August 20, 2012 Reading the actual problem, wouldn't it make more sense to use something like soundex() to compare the search string and the searched field? Or search for each word, and the rows which have the most matches are at the top of the list. (So if it matches 3/4 words it is a good match) Quote Link to comment Share on other sites More sharing options...
Barand Posted August 20, 2012 Share Posted August 20, 2012 I'm with Psycho - FULLTEXT Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 20, 2012 Share Posted August 20, 2012 If you don't want FULL TEXT searching, then a better solution is to simply explode the search string into separate words and create multiple LIKE clauses. Since these are product names you can just look for results where all the words are included and not worry about the order at all. So, your query would look something like this SELECT * FROM table_name WHERE field_name LIKE '%word1%' AND field_name LIKE '%word2%' AND field_name LIKE '%word3%' AND field_name LIKE '%word4%' Here's one way to code that: $search_string = trim($_POST['search']); $search_words = array_filter(explode(' ', $search_string)); foreach($search_words as $idx => $word) { $search_words[$idx] = "field_name LIKE '" . mysql_real_escape_string($word) "'"; } $query = "SELECT * FROM table_name WHERE " . implode(' AND ', $search_words); Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 20, 2012 Share Posted August 20, 2012 Exactly Quote Link to comment Share on other sites More sharing options...
shoeyn Posted August 20, 2012 Author Share Posted August 20, 2012 Only problem is that he as over 120,000 products and lots of them have similar words in the name, but he knows that the certain words he searches for are in the name, just doesn't know where. Searching for each word individually will bring up loads of other products as well, which won't help him find the certain product. Is there a way to do a MYSQL query saying that it must contain certain words, just in any order? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 20, 2012 Share Posted August 20, 2012 Only problem is that he as over 120,000 products and lots of them have similar words in the name, but he knows that the certain words he searches for are in the name, just doesn't know where. Searching for each word individually will bring up loads of other products as well, which won't help him find the certain product. Is there a way to do a MYSQL query saying that it must contain certain words, just in any order? Reread my previous response - the code I provided would generate a query that does just that. Now, had I used OR instead of AND, the query would match any record where one or more of the words were included. But, I did use AND, so the query will only match records were every word in the search string appears in the record. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.