2014-07-17 2 views
0

안녕하세요,PHP와 MySQL의 라이브 검색

데이터베이스에서 가져온 데이터 목록을 배열로 가져 오는 페이지가 있습니다. 여기 코드는 다음과 같습니다

<table width="100%"> 
<thead> 
<tr> 

<th>Name:</th> 
<th>Address:</th> 
<th>Birthday:</th> 

</tr> 
</thead> 
$samp=mysql_query("select * from client order by id asc"); 
$counter=0; 
while($row=mysql_fetch_array($samp)) 
    { 
    $id   = $row['id']; 
    $name  = $row['name']; 
    $address = $row['address']; 
    $birthday = $row['birthday']; 


     if($counter%2) 
     { 
     ?> 
     <tbody> 
     <tr id="<?php echo $id; ?>"> 
     <?php } else { ?> 
     <tr id="<?php echo $id; ?>"> 
     <?php } ?> 
     <td> 
     <span class="text"><?php echo $id.". ".$name; ?></span> 
     </td> 
     <td> 
     <span class="text"><?php echo $address; ?></span> 
     </td> 
     <td> 
     <span class="text"><?php echo $birthday; ?></span> 
     </td> 
     <tr> 
     <!--and so on --> 
     </tbody> 

내가이 참조를 Live Search Jquery보고이 거 내 프로그램 작동하는지 노력했다. 놀랍게도 미리 정의 된 값에 대해서만 작동하는 것으로 보입니다. 그러나이 라이브 검색 기능이있는 테이블에 사용하려고 할 때 작동하지 않았습니다.

이 참조가 실제로 달성하려고하는 것이므로 너무 나쁘다. 사용자가 검색을 완료 할 때까지 기다리지 않고 이미 단어를 검색하기 때문입니다. 의견이있는 사람은 누구입니까?

+0

내가 힘든 시간을 귀하의 질문을 이해하는 데 문제가 있습니다 . 정확히 당신이 성취하고자하는 것은 무엇입니까? 코드는 당신이 원하는 것을 어디서하지 않습니까? 당신은 무엇을 기대 했습니까? 실제로는 어떻게됩니까? – Caweren

+0

@Caweren 사용자가 검색을 입력하면 검색 기능을 실행하고 사용자가 찾고있는 필드를 db에서 호출하는 검색 상자를 만드는 것이 시도됩니다. 위에 게시 한 링크처럼. 그러나 그것을 시험해 보았을 때 효과가 없었습니다. 그래서 저는 아마도 db에서 가져온 컬럼에 값을 배치했기 때문일 것이라고 생각했습니다. 코드는 사전 정의 된 값에 대해서만 작동하는 것 같습니다. – MoonPrincess

답변

0

테스트를 :

index.html을

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<script> 
function showResult(str) 
{ 
if (str.length==0) 
    { 
    document.getElementById("livesearch").innerHTML=""; 
    return; 
    } 

    xmlhttp=new XMLHttpRequest(); 

xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    document.getElementById("livesearch").innerHTML=xmlhttp.responseText; 
    } 
    } 
xmlhttp.open("GET","livesearch.php?q="+str,true); 
xmlhttp.send(); 
} 
</script> 
<title>main page</title></head> 
<body> 
<form> 
<input type="text" size="30" onkeyup="showResult(this.value)"> 
<div id="livesearch"></div> 
</form> 

</body> 
</html> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 

<?php 
$xmlDoc=new DOMDocument(); 
$xmlDoc->load("links.xml"); 

$x=$xmlDoc->getElementsByTagName('link'); 

//get the q parameter from URL 
$q=$_GET["q"]; 

//lookup all links from the xml file if length of q>0 
if (strlen($q)>0) 
{ 
$hint=""; 
     for($i=0; $i<($x->length); $i++) 
     { 
     $y=$x->item($i)->getElementsByTagName('title'); 
     $z=$x->item($i)->getElementsByTagName('url'); 
       if ($y->item(0)->nodeType==1) 
        { 
       //find a link matching the search text 
         if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) 
          { 
           if ($hint=="") 
            { 
             $hint="<a href='" . 
             $z->item(0)->childNodes->item(0)->nodeValue . 
             "' target='_blank'>" . 
             $y->item(0)->childNodes->item(0)->nodeValue . "</a>"; 
            } 
           else 
            { 
             $hint=$hint . "<br /><a href='" . 
             $z->item(0)->childNodes->item(0)->nodeValue . 
             "' target='_blank'>" . 
             $y->item(0)->childNodes->item(0)->nodeValue . "</a>"; 
            } 
          } 
        } 
     } 
} 

// Set output to "no suggestion" if no hint were found 
// or to the correct values 
if ($hint=="") 
    { 
     $response="no suggestion"; 
    } 
else 
    { 
     $response=$hint; 
    } 

//output the response 
echo $response; 
?> 

</body> 
</html> 

livesearch.php links.xml

<?xml version="1.0" encoding="utf-8"?> 
<links> 
    <link> 
     <title>about</title> 
     <url>main.html</url> 
    </link> 

    <link> 
     <title>Gmail</title> 
     <url>www.google.com</url> 
    </link> 

    <link> 
     <title>yahoo</title> 
     <url>www.google.com</url> 
    </link> 

    <link> 
     <title>bing</title> 
     <url>www.google.com</url> 
    </link> 

    <link> 
     <title>torrent search</title> 
     <url>www.google.com</url> 
    </link> 

    <link> 
     <title>Zend</title> 
     <url>www.google.com</url> 
    </link> 
</links> 
+0

livesearch.php에서 mysql 구문을 사용하여 값을 반향시킬 수 있습니다. –