views:

707

answers:

2

I am trying to have a search result for someones name exploded then the first name and last name each search against each field 'first' and 'last'.

But need to leave room what if someone search for first last midle name like = $who = James David Smith it will exploded it and search each of the names against the first column and the last column. Incase the user inputs the name lastname first or adds a middle name.

Here is what i have so far but im stuck. Help. Please.

<?

$search = explode(' ', $who);

$limit=verify($get['page'])?" limit ".($get['page']*10).",10":" limit 10";

$q="select * from users where (first IN('%$search[]%') or last IN('%$search[]%') or full_name LIKE '%".$get['who']."%') AND (city LIKE '%".$get['where']."%' or province LIKE '%".$get['where']."%') order by province";        

$rows=get_recordset($q);

if(count($rows)){
    foreach($rows as $row){

echo $q;

?>
+2  A: 

I am not sure I got you but if what you mean is :

When you have 3 names then - check all names against all three fields (first,last,fullname).

When you have only 2 names then - check first name against first field and last name against last field

then this should get it done:

$search = explode(' ', $who);
if (count($search) >2) { // in case we do got a middle name - 
    $whereNameStringsArr = array();
    foreach ($search as $val) {
     $whereNameStringsArr[] =  " first LIKE '%$val%' 
         OR last LIKE '%$val%' 
         OR full_name LIKE '%$val%' ";
    }
    $whereNameClause = implode(" OR ",$whereNameStringsArr);
} else { // in case we don't got a middle name
    $whereNameClause =  " first LIKE '%{$search[0]}%' 
       OR last LIKE '%{$search[1]}%' ";
}
$limit=verify($get['page'])?" limit ".($get['page']*10).",10":" limit 10";
$q="select * from users where  ($whereNameClause) 
       AND (city LIKE '%".$get['where']."%' or province LIKE '%".$get['where']."%') 
       order by province
       $limit";        

$rows=get_recordset($q);
....

Of course - make sure you validate all data you get from user's input

Saggi Malachi
Close. I like where youa re going but doesnt work. If you reverse the order of the persons name and search last name first. Returns nothing.
A: 

So why not do a fuzzy search? When you explode the data, do something like this:

$name = $_POST['name'] // John Michael Smith

$first_name = substr($name, 0, strrpos($name, ' ')); //Returns everything before last space, ie John Michael

$last_name = strstr($name, ' '); //Returns everything after first space, ie John Michael

Now you just run the mysql query but set it to look for anything that contains the above values (substring search) using LOCATE:

$results = mysql_query("SELECT * FROM names 
                WHERE LOCATE(firstname, '$first_name') > 0 OR 
                      LOCATE(lastname, '$last_name')   > 0
                ORDER BY lastname

I'll assume you know what to do with the results.


Quick Update, If you are worried about users entering in Last Name First Name Middle, or what have you, you could always use the above but do a locate on the entire string:

$results = mysql_query("SELECT * FROM names 
                WHERE LOCATE(firstname, '$name') > 0 OR 
                      LOCATE(lastname, '$name')   > 0
                ORDER BY lastname

This will check both columns for any instances of any of the string. You should probably (after sanitizing the string, as was rightly suggested already) also remove any commas (but not hyphens) in case someone enters last,first or what have you. replace them with spaces and then reduce all multi-spaces to one space to be double sure.


Sorry about that. Here is the way to split the names (by blanks). There as a problem with my substr syntax:

 $name = $_POST['name'] // John Michael Smith

 $first_name = substr($name, 0, strrpos($name, ' ')); //Returns everything before last space, ie John Michael

 $last_name = strstr($name, ' '); //Returns everything after first space, ie John Michael
Anthony
Syntax error on line $first_name=substr(0,strrchr($name, " ")); //Returns everything before last space, ie John Michael
Parse error: syntax error, unexpected T_VARIABLE
Sorry. That's what I get for copying and pasting from documentation examples. This is how to get the first name:`$first_name = substr($name, 0, strrpos($name, " "));`
Anthony
Nope. Now its not returning any results.
Well the way it works is `LOCATE(substr, str)` returns the postition of the substring within the string. so if you are doing `LOCATE(mysql_name, $php_name) > 0` the mysql name should be the smaller of the two, right? and thus should be higher than 0, right? Did you try just doing a search for only the first name? It could be that it should be AND instead of OR in the sql query. Have you confirmed that the php bit at the start is catching just the parts I said it should?
Anthony