Hi!
I'm trying to do something but im not too sure if it can be done (easily). What I am trying to do is take an array filled with values, and taking some form inputs from a HTML form, and searching the array to see which entries contain all the words searched for. So far I can get it to search the array for single value strings, which is fine, but when i add multiple words in the string, i seem to become stuck.
Here is what I am using so far.
PHP Code:
$query_array=explode(" ", $query_string); // split the query into single terms
if ($query_array[0] !="") // check 1st term isnt empty.
{
if (preg_match ("/$query_array[0]/i", $database_record))
{
echo $strRecord;
}
else
{
echo "A match was not found.";
}
}
}
All good so far. Now what i want it to do is to search for each term submitted. I want to do this using multiple preg_match's but dont know how to do it. For example if the user submits 5 terms to search for I would need something like:-
PHP Code:
if (preg_match ("/$query_array[0]/i", $database_record) && preg_match ("/$query_array[1]/i", $database_record) && preg_match ("/$query_array[2]/i", $database_record) && preg_match ("/$query_array[3]/i", $database_record) && preg_match ("/$query_array[4]/i", $database_record) )
etc....
Now this would work if i knew the user was searching for a fixed number of terms, but we all know that wont happen!! so will i need to use multiple if statements such as...
PHP Code:
$i=0;
if (preg_match ("/$query_array[$i]/i", $database_record))
{
$i++;
if (preg_match ("/$query_array[$i]/i", $database_record))
{
$i++;
if (preg_match ("/$query_array[$i]/i", $database_record))
// more and more will need to go here for each term searched for, but how
// do I get it to loop for each term?? If I have to account for each possibility
// then will i need to hard write these steps over and over 100 times??
{
echo"match found";
}
}
}
else
{
echo"no match";
}
I am hoping that I am being clear in all this, and hope some die hard coders can help because this is really screwing with my head!!!
Many thanx guys!