Jump to content

Possible combinations function


shoeyn

Recommended Posts

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%

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.