Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Malfoy

macrumors 6502a
Original poster
Nov 1, 2005
688
2
What I'm trying to do is make a search function that if given the info will search the database and remove replicas and display the results.

First the search info is taken from the text box and put into an array (jo was the subject of the search):
PHP:
$Search = array_unique( explode(" ", mysql_real_escape_string($_POST['Search'])));

A counter is setup so it know how many times to run the search loop
PHP:
$SearchParamCount = count($Search);

The Counter is set to 0
PHP:
$Counter = 0;

Then the primary stuff happens:
PHP:
	$WaitNameResults = array();
	while ( $Counter < $SearchParamCount) {
	$Current = $Search[$Counter];
		//$WaitNameObtain = "SELECT  Fname, Lname, RLID, WID FROM Wait WHERE (Fname LIKE '%" .$Current . "%') OR (Lname LIKE '%" . $Current . "%') ";
		$WaitNameObtain = "SELECT  Fname, Lname, RLID, WID FROM Wait WHERE (Fname LIKE '%$Current%') OR (Lname LIKE '%$Current%') ";
		$WaitNameQuery = mysql_query($WaitNameObtain);
		while ($WaitNameResult = mysql_fetch_array($WaitNameQuery)){
			echo ' Fname and Lname is  from fetch array is ' . $WaitNameResult['Fname'] . ' ' . $WaitNameResult['Lname'] . '<br>';
			print_r($WaitNameResult);
			echo ' is the print_r version <br>  print_r of WaitNameResults is: ';
			print_r($WaitNameResults);
			echo '<br>';
			$WaitNameResults = $WaitNameResults + $WaitNameResult;
		}
		$Counter++;
	
	} // ends while loop
   // $WaitNameResults = array_unique($WaitNameResults);
	
 	echo '<br>  The final contents of WaitNameResults is: ';
 	print_r($WaitNameResults);
	$WaitNameCount = count($WaitNameResults);
} //ends else


This is where I'm dropping the ball:
PHP:
$WaitNameResults = $WaitNameResults + $WaitNameResult;

What I would like it do is APPEND/ADD the current array contents to the array $WaitNameResults but its not.

This is what it outputs:

this is the search param count 1
Fname and Lname is from fetch array is John Smith
Array ( [0] => John [Fname] => John [1] => Smith [Lname] => Smith [2] => 1298 [RLID] => 1298 [3] => 3 [WID] => 3 ) is the print_r version
print_r of WaitNameResults is: Array ( )
Fname and Lname is from fetch array is Johnny Drama
Array ( [0] => Johnny [Fname] => Johnny [1] => Drama [Lname] => Drama [2] => 592 [RLID] => 592 [3] => 15 [WID] => 15 ) is the print_r version
print_r of WaitNameResults is: Array ( [0] => John [Fname] => John [1] => Smith [Lname] => Smith [2] => 1298 [RLID] => 1298 [3] => 3 [WID] => 3 )
Fname and Lname is from fetch array is Jon Jon
Array ( [0] => Jon [Fname] => Jon [1] => Jon [Lname] => Jon [2] => 592 [RLID] => 592 [3] => 21 [WID] => 21 ) is the print_r version
print_r of WaitNameResults is: Array ( [0] => John [Fname] => John [1] => Smith [Lname] => Smith [2] => 1298 [RLID] => 1298 [3] => 3 [WID] => 3 )
Fname and Lname is from fetch array is Jon Jon
Array ( [0] => Jon [Fname] => Jon [1] => Jon [Lname] => Jon [2] => 592 [RLID] => 592 [3] => 22 [WID] => 22 ) is the print_r version
print_r of WaitNameResults is: Array ( [0] => John [Fname] => John [1] => Smith [Lname] => Smith [2] => 1298 [RLID] => 1298 [3] => 3 [WID] => 3 )
Fname and Lname is from fetch array is Jon Jon
Array ( [0] => Jon [Fname] => Jon [1] => Jon [Lname] => Jon [2] => 592 [RLID] => 592 [3] => 23 [WID] => 23 ) is the print_r version
print_r of WaitNameResults is: Array ( [0] => John [Fname] => John [1] => Smith [Lname] => Smith [2] => 1298 [RLID] => 1298 [3] => 3 [WID] => 3 )
The final contents of WaitNameResults is: Array ( [0] => John [Fname] => John [1] => Smith [Lname] => Smith [2] => 1298 [RLID] => 1298 [3] => 3 [WID] => 3 )

From this I know a few things:
1. The SQL is fine, its pulling the results into mysql_fetch_array
2. $WaitNameResult has the current info each time during the loop
3. $WaitNameResults = $WaitNameResults + $WaitNameResult; Worked at the begining changing the contents of $WaitNameResults from Array() to the first iteration of $WaitNameResult.
4. It doesn't append/add anything after that.


i tried using array_merge but got the same unsuccessful result. Again what I'm trying to do is get each of those lines into an array so I can run array_unique on the end result and then display them. using SELECT DISTINCT at the mysql level doesn't solve the problem because a diff query is being run for each word from the explode so the duplication check has to be ran after all the data is accumulated. Any help would be appreciated. thanks.
 
What are you trying to accomplish? I cannot tell what your end result is?

I have found however that if I'm doing some kind of fancy searches where you are doing something like %$search% it is often easiest to do them by doing something like this in the query

SELECT *, CONCAT(Fname, ' ', Lname) AS 'name' FROM Wait HAVING name LIKE '%$search%' ORDER BY Lname LIMIT 20

This makes it MUCH easier to include searches for multiple words and the like.

But I'm not all that sure what you are going for, so I'm not sure how to help.
 
Lets say i'm trying to search for jennifer gardner.
At the moment i explode jennifer gardner into jennifer and gardner.

when i query for results (because my db has a first name and last name fields) the first query is going to find her under jennifer and the second is going to find her under gardner.

What I want is to after the first query to store jennifer gardner's (and whoever else came up) info into an array(lets call it $storage).

Then after the second query[gardner search] runs I want the info it finds to be appended/added to the the array ($storage).

once that is done I want to be able to run
$slimstorage =array_unique($storage);

this would remove all duplicates including getting rid of the duplicate jennifer gardner.

once I have that new array, I will create a loop and use it for various things.
 
$search is the search string that comes from whatever source, it is multiple words separated by spaces.

Code:
//this turns the incoming search into an array
$search = explode(" ", $search);
// turns the new search array back into a string putting some SQL stuff around it
$search_separated = "name LIKE '%" . implode("%' AND name LIKE '%", $search) . "%'";

// put that into the query
$search_query = mysqli_query ("SELECT *, CONCAT(Fname, ' ', Lname) AS 'name'
FROM Wait
HAVING $search_separated
ORDER BY Lname LIMIT 20");

This will accomplish a search where both the first and the last name need to include BOTH words that you typed in. The LIMIT is important if your database is relatively large, and somebody searches for something like "n" or "a" then the query %a% will return MOST of the database slowing everything down. (made that mistake before)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.