i have added google keyword suggest in my search field but dont know why if i use 1 word like "hi" or "hello" then result show without any problem but if i use space or two word like "hi coll" or "hello my" then result doesnt show. please tell me how to fix it.
check below code.
demo
http://174.132.200.244/hotbox/demo/searchsuggest2/sp/
here is google keyword suggestion code
$q = strtolower($_GET["q"]);
if (!$q) return;
$len = strlen($q);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$aResults = array();
$count = 0; $i=0;
$url = "http://suggestqueries.google.com/complete/search?qu=" . $q;
$text = file_get_contents($url);
$text = str_replace('window.google.ac.h(["' . $q . '",[[', '', $text);
$arr_items = explode('],[', $text);
foreach($arr_items as $items) {
$arr_item = explode(',', $items);
$key = $arr_item[0];
$key = trim($key, '"');
if (strpos(strtolower($key), $q) !== false) {
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($key) );
}
here is full code of my php
<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$len = strlen($q);
$limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 0;
$aResults = array();
$count = 0; $i=0;
$url = "http://suggestqueries.google.com/complete/search?qu=" . $q;
$text = file_get_contents($url);
$text = str_replace('window.google.ac.h(["' . $q . '",[[', '', $text);
$arr_items = explode('],[', $text);
foreach($arr_items as $items) {
$arr_item = explode(',', $items);
$key = $arr_item[0];
$key = trim($key, '"');
if (strpos(strtolower($key), $q) !== false) {
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($key) );
}
if ($limit && $count==$limit)
break;
$i++;
}
/* if ($len)
{
for ($i=0;$i<count($aUsers);$i++)
{
// had to use utf_decode, here
// not necessary if the results are coming from mysql
//
if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $q)
{
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
}
if ($limit && $count==$limit)
break;
}
} */
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0
if (isset($_REQUEST['json']))
{
header("Content-Type: application/json");
echo "{\"results\": [";
$arr = array();
for ($i=0;$i<count($aResults);$i++)
{
$arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"\"}";
}
echo implode(", ", $arr);
echo "]}";
}
else
{
header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
for ($i=0;$i<count($aResults);$i++)
{
echo "<rs id=\"".$aResults[$i]['id']."\" info=\"".$aResults[$i]['info']."\">".$aResults[$i]['value']."</rs>";
}
echo "</results>";
}