å ¥åãã©ã¼ã ã«å ¥åè£å®æ©è½ãå®è£ ãã
å ¥åãã©ã¼ã ã«å ¥åè£å®æ©è½ãå®è£ ãã
å
¥åãã©ã¼ã ã«googleã®æ¤ç´¢ãã©ã¼ã ã®ãããªå
¥åè£å®æ©è½ãå®è£
ããã
サンプルページ
jQueryã®Autocompleteãã©ã°ã¤ã³ã使ç¨ããã¨ç°¡åã«å®è£
ã§ããã(ä¸è¨2ã¤ã®ãã¡ã¤ã«)
http://view.jquery.com/trunk/plugins/autocomplete/jquery.autocomplete.css
http://view.jquery.com/trunk/plugins/autocomplete/jquery.autocomplete.js
jQueryæ¬ä½ã¯Googleã«ãã¹ãã£ã³ã°ããã¦ãããã®ã使ç¨ã
http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js
index.html
<!DOCTYPE html> <html lang="ja"> <head> <title>å ¥åè£å®ãµã³ãã«</title> <meta charset="UTF-8"> <script type="text/javascript" src= "http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <script type="text/javascript" src= "jquery.autocomplete.js"></script> <link type="text/css" href="jquery.autocomplete.css" media="all" rel="stylesheet" /> <script type="text/javascript"> $(document).ready(function(){ $('#in_text').autocomplete('suggest.php').result( function(event, item) { item[0]; } ); }); </script> </head> <body> <input type="text" size="30" name="in_text" id="in_text"/> </body> </html>
autocomplete('suggest.php')ã«äºæ¸¬å¤æåè£ã®ãªã¹ããåºåããã¹ã¯ãªãããæå®ã
ä»åã¯phpã§å®è£
ã
suggest.php
jQuery Autocompleteã«ã¯äºæ¸¬å¤æåè£ã®ãªã¹ããæ¹è¡åºåãã§æ¸¡ãã°ããã
suggest.phpã«ãã®æ©è½ãå®è£
ããã
äºæ¸¬å¤æåè£ã®ãªã¹ãåå¾å
ã¯Google Suggest APIã使ç¨ããã
<?php require_once("HTTP/Request.php"); $http_request = new HTTP_Request(); //ãã©ã¡ã¼ã¿ã«ds=ytã追å ããã¨YouTubeã®äºæ¸¬å¤æåè£ã®ãªã¹ããåå¾ã§ãã $http_request->setURL("http://suggestqueries.google.com/complete/search?hl=ja&client=firefox&json=t&q=".$_GET["q"]); $http_request->sendRequest(); $result = mb_convert_encoding($http_request->getResponseBody(), "UTF-8", "SJIS");//çµæãSJISã§æ»ã£ã¦ãããjson_decode()ã¸ã¯UTF-8ã§æ¸¡ãå¿ è¦ãããããå¤æã $result = json_decode($result); echo implode("\n", $result[1]); ?>