これを使用して値とキーを検索する多次元配列があります
<input type="text" onkeyup="showHint(this.value)"></input>
これと一緒に
function showHint(str)
{
if (str.length==0)
{
document.getElementById("nav").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("nav").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
私はphpビットに少しこだわっています。配列は次のようになります (短縮)
Array
(
[Modest Mouse] => Array
(
[The Moon & Antarctica] => Array
(
[0] => 3rd Planet
[1] => Gravity Rides Everything
[2] => Dark Centre of the Universe
)
[The Lonesome Crowded West] => Array
(
[7] => Cowboy Dan
[8] => Trailer Trash
[9] => Out of Gas
)
[The Vasco Era] => Array
[Lucille] => Array
(
[0] => Not Stuck Here
[1] => For No One
)
)
)
クエリを取得することから始めます
$q=$_GET["q"];
それから私はこれを行うことができます
if (strlen($q) > 0)
{
$hint="";
foreach($a as $b => $c)
{
if (strtolower($q)==strtolower(substr($b,0,strlen($q))))
{
if ($hint=="")
{
$hint=$b;
}
else
{
$hint=$hint." , ".$b;
}
}
}
}
Modest Mouse や The Vasco Era を手に入れることはできますが、それ以上のことはできません。入力フィールドに「T」と入力すると、「The Moon & Antartica」、「The Lonesome Crowded West」、「Trailer Trash」、および「The Vasco Era」という結果が得られます。