PHP:如果没有名字

时间:2010-11-05 11:54:46

标签: php

所以我有这个PM系统。

在收件人字段中,您可以输入要写入的全名。

所以如果他不记得名字,我想帮助用户。

所以如果你想写全名“Megan Fox”并且只知道:

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

它将返回“你的意思是梅根福克斯吗?” 这已经很好了。

现在我需要的东西在这里,如果你只写福克斯,没有名字,我希望它回应“你的意思是梅根福克斯吗?”

这是我卡住的地方,因为它在空格后创建$ lastname。而且我真的不想在第一个和最后一个使用两个字段。

我该怎么做?

这是我的代码:

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 = '$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"]." ?";
 }

3 个答案:

答案 0 :(得分:2)

这应该有用。

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

答案 1 :(得分:2)

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

答案 2 :(得分:1)

我想最好匹配单个值而不是两个。例如,很难猜出名字在哪里以及最后在哪里,因为用户可以输入Magan Fox或Fox Megan。

所以查询看起来像这样:

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