2011-11-13 2 views
0

hello friends 나는 ajax와 mysql의 도움으로 실시간 검색을하고있다. 문제는 우리가 무언가를 쓸 때 알파벳 "a"라고 가정하면 실사 결과를 낳지 만 공백을 넣은 다음 "a"라고 가정하면 아무 결과도 나타나지 않습니다. 검색에서 공백을 무시할 수있는 방법은 무엇입니까 ?? 내 코드는mysql ajax search query

아약스 search.php

<script type="text/javascript"> 
    $(document).ready(function(){ 

    $(".search").keyup(function() 
{ 
    var searchbox = $(this).val(); 
    var dataString = 'searchword='+ $.trim(searchbox); 

    if(searchbox=='') 
{ 
$('#display123').hide(); 
} 

else 
{ 

$.ajax({ 
type: "POST", 
    url: "friends/search1.php", 
    data: dataString, 
    cache: false, 
    success: function(html) 
    { 
    $("#display123").html(html).show(); 
    } 
    }); 
    }return false;  


    }); 
    }); 
    </script> 

MySQL의 search1.php

  <?php include($_SERVER["DOCUMENT_ROOT"]."/sexy/include/connection.php"); 
    if($_POST) 
    { 
     $q=$_POST['searchword']; 
    $sql_res=mysql_query("select * from users_profile where fname like '%$q%' or  Email like '%$q%' order by rand() LIMIT 10"); 
     while($row=mysql_fetch_array($sql_res)) 
    { 
     $fname=$row['fname']; 
    $lname=$row['lname']; 
    $img=$row['profile_pic']; 
    $country=$row['country']; 
     $uid=$row['uid']; 

      $re_fname='<b>'.$q.'</b>'; 
    $re_lname='<b>'.$q.'</b>'; 

    $final_fname = str_ireplace($q, $re_fname, $fname); 

     $final_lname = str_ireplace($q, $re_lname, $lname); 


    ?> 

      <div class="display_box" > 
     <span><a href="profile.php?f_id=<?php echo $uid;?>"><img src="<?php echo $img; ?>"    width="50" height="50" style="float:left; margin-right:6px" /></a></span> 
    <div class="searchtext"><a href="profile.php?f_id=<?php echo $uid;?>"><?php echo    $final_fname; ?>&nbsp;<br/> 
    <span style="font-size:10px; color:#999999"><?php echo $country; ?></span></a></div></div> 
    <?php 
    } 
     } 
    else 
    { 
    } 
    ?> 

답변

0

당신은 같은 쿼리에서 변수를 사용하기 전에 공백을 트리밍 할 수 있습니다

 
$q= trim($_POST['searchword']); //remove whitespaces from front and end 
+0

답변 해 주셔서 감사합니다.하지만 이것을 사용하여 검색 상자에 공백을 넣으면 모든 알파벳을 입력하기 전에 모든 알파벳의 모든 레코드를 가져 오는 것을 보여줍니다. –

+0

실시간 검색을 구현 ​​한 결과 검색 상자의 'LEN()'이 1 (또는 2) 문자 인 경우 검색을 실행하지 않는 것이 좋습니다. 대부분의 경우 검색은 유용하지 않습니다. –

+0

이 코드를 도와 드릴까요? –