2

第一个问题是我有一个自动完成功能,当我完成输入名字时,它可以正常工作,但是当我按空格键继续输入姓氏时,div 会隐藏并且不会继续搜索结果。我遗漏了一些东西在 php 文件或 jquery 文件中。

这是搜索栏

<form method='post' action='index2.php#profile_info.php'>
    <input type="text"  id='inputSearch' placeholder="Search"  autocomplete="off" class="search" />
    <input type='submit' id='Search_Submit' value='' />
</form>
<div id="divResult"</div> 

这是查询数据库的 search.php 文件

<?php
include('connect.php');
if($_POST)
{
$q=$_POST['searchword'];
$sql_res=mysql_query("SELECT user_id, firstname, lastname, email, country, gender from users where firstname like '%$q%' OR lastname like '%$q%' order by email_activated='1' LIMIT 5");
while($row=mysql_fetch_array($sql_res))
{

$id = $row['user_id'];  
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$email=$row['email'];
$country=$row['country'];
$Gender=$row['gender'];
$cacheBuster = rand(999999999,9999999999999);

$check_pic = "members/$id/image01.jpg";
    $default_pic = "members/image01.jpg";
    if (file_exists($check_pic)) {
    $users_pic ="$check_pic?$cacheBuster"; // forces picture to be 100px wide and no more
    }else {
    $users_pic = "images/profiles/".$Gender."_small.jpg"; // forces default picture to be 100px wide and no more

}

$b_firstname='<b>' .$q. '</b>';
$b_lastname='<b>'.$q.'</b>';
$final_firstname = str_ireplace( $q, $b_firstname, $firstname);
$final_lastname = str_ireplace( $q, $b_lastname, $lastname);
?>
<div class="display_box" align="left" >
<a href="http://localhost/MyNewSite/index2.php?user_id='<?php echo $id; ?>'#status.php" target="_self"> <img src="<?php echo $users_pic; ?>" style="width:55px; height:50px; float:left; margin-right:6px;" /></a><span class="name"><a href="http://localhost/MyNewSite/index2.php?user_id='<?php echo $id; ?>'#status.php" target="_self" class="friendsLink"><?php echo " $final_firstname $final_lastname"; ?> </a></span>&nbsp;<br/><span style="font-size:10px; margin-left:6px;"><?php echo $email; ?></span><br/>
<span style="font-size:10px; color:#C40000; margin-left:6px;"><?php echo $country; ?></span></div>

<?php
}
}
?>

和 jquery 加载 div

$(function(){
$(".search").keyup(function() 
{ 
var inputSearch = $(this).val();
var dataString = 'searchword='+ inputSearch;
if(inputSearch!='')
{
    $.ajax({
    type: "POST",
    url: "search.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
    $("#divResult").html(html).show();
    }
    });
}return false;    
});

jQuery("#divResult").live("click",function(e){ 
    var $clicked = $(e.target);
    var $name = $clicked.find('.name').html();
    var decoded = $("<div/>").html($name).text();
});
jQuery(document).live("click", function(e) { 
    var $clicked = $(e.target);
    if (! $clicked.hasClass("search")){
    jQuery("#divResult").fadeOut(); 
    }
});
$('#inputSearch').click(function(){
    jQuery("#divResult").fadeIn();
});
});


jQuery(function($){
   $("#inputSearch").Watermark("Search");
   });
4

2 回答 2

0

这是 where 查询,非常感谢 Michal Klouda 提供的问题所在的提示。

$sql_res=mysql_query("SELECT user_id, firstname, lastname, email, country, gender
FROM users where lower(concat_ws(' ', firstname, lastname)) like '%$q%' ORDER BY email_activated =  '1'
LIMIT 5");
于 2012-10-21T01:56:11.883 回答
0

当您在名字后键入空格时,它不会返回任何内容,因为搜索短语按原样传递,并分别在firstnamelastname列中查找。查看您的查询:

SELECT user_id, firstname, lastname, email, country, gender 
from users 
where firstname like '%$q%' OR lastname like '%$q%'  /* <- here's the problem */
order by email_activated='1' LIMIT 5

如果您的表中有 John Smith,则不会找到他,因为"John" 不像 "%John %".

您将需要更改 where 子句。你可以试试:

where firstname + ' ' + lastname like '%$q%' or
      lastname + ' ' + firstname like '%$q%'
于 2012-10-16T14:15:08.027 回答