开发者

PHP: If no first name

So i have this PM system.

At the recipient field, You can type in the full name of who you want to write to.

So I want to help the user alittle, if he don't remember the name.

So if you want to write the full name "Megan Fox" and only know:

Megan
Megan F
Megan Fo
Meg Fo
M Fox
etc..

It will return "Did you mean Megan Fox?" This already works fine.

Now what I need In this is that if you write only Fox , with no firstname, i want it to echo "Did you mean Megan Fox?"

Here's where Im stuck, as it creates $lastname after the space. And i really don't want to use two fields for first and last.

How can I do that?

Here's my code:

list($firstname, $lastname) = array_map('ucfirst', explode(' ', $mot, 2));
$qur = mysql_query("
 SELECT id, firstname, lastname, 
 (firstname = '$firstname' AND lastname = '$lastname') AS full FROM users 
 WHERE (firstname开发者_JS百科 = '$firstname' AND lastname='$lastname') 
 OR (firstname LIKE '$firstname%' AND lastname LIKE '$lastname%')
 ORDER BY (firstname = '$firstname' AND lastname='$lastname') DESC");
 $get = mysql_fetch_array($qur);

 if($get["full"] == 1){
 echo $get["id"];
 }else{
 echo "Did you mean: ".$get["firstname"]." ".$get["lastname"]." ?";
 }


This should work.

$qur = mysql_query("
 SELECT id, firstname, lastname, 
 (firstname = '$firstname' AND lastname = '$lastname') AS full FROM users 
 WHERE (firstname = '$firstname' AND lastname='$lastname') 
 OR (firstname LIKE '$firstname%' AND lastname LIKE '$lastname%')
 OR ('$lastname' = '' AND lastname LIKE '$firstname%')
 ORDER BY (firstname = '$firstname' AND lastname='$lastname') DESC");


OR ('$lastname'='' AND lastname LIKE '$firstname%')


I guess it is better to match against single value instead of two. For example it is hard to guess where is first name and where is last as user can type Magan Fox or Fox Megan.

so query would look like this:

SELECT id, firstname, lastname, CONCAT (firstname, ' ', lastname) AS fullname
FROM `users` 
WHERE firstname LIKE '{$string}%' OR lastname LIKE '{$string}%'
GROUP BY firstname, lastname
ORDER BY firstname ASC, lastname ASC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜