So I am trying to make a search bar with IMDB Suggestion and this is where I got so far. This is my Suggest.php file.
<?php
try{
$term = trim(strtolower($_REQUEST['term']));
$search = str_replace(array(" ", "(", ")"), array("_", "", ""), $term); //format search term
$firstchar = substr($search,0,1); //get first character
$url = "http://sg.media-imdb.com/suggests/${firstchar}/${search}.json"; //format IMDb suggest URL
$jsonp = @file_get_contents($url); //get JSONP data
preg_match('/^imdb\$.*?\((.*?)\)$/ms', $jsonp, $matches); //convert JSONP to JSON
$json = $matches[1];
$arr = json_decode($json, true);
$s = array(); //New array for jQuery Autocomplete data format
if(isset($arr['d'])){
foreach($arr['d'] as $d){
$img = preg_replace('/_V1_.*?.jpg/ms', "_V1._SY50.jpg", $d['i'][0]);
$s[] = array('label' => $d['l'], 'value' => $d['id'], 'cast' => $d['s'], 'img' => $img, 'q' => $d['q']);
}
}
header('content-type: application/json; charset=utf-8');
echo json_encode($s); //Convert it to JSON again
} catch (Exception $e){
//Do nothing
}
?>
Here is my imdbImage.php file:
<?php
header("Content-type: image/jpeg");
//URL for IMDb Image.
$url = rawurldecode($_REQUEST['url']);
echo file_get_contents($url);
?>
And finally the script:
<script type="text/javascript">
$(function(){
$("#q").focus(); //Focus on search field
$("#q").autocomplete({
minLength: 0,
delay:5,
source: "/inc/suggest.php",
focus: function( event, ui ) {
$(this).val( ui.item.value );
return false;
},
select: function( event, ui ) {
$(this).val( ui.item.value );
return false;
}
}).data("uiAutocomplete")._renderItem = function( ul, item ) {
return $("<li></li>")
.data( "item.autocomplete", item )
.append( "<a>" + (item.img?"<img class='imdbImage' src='/inc/imdbImage.php?url=" + item.img + "' />":"") + "<span class='imdbTitle'>" + item.label + "</span>" + (item.cast?"<br /><span class='imdbCast'>" + item.cast + "</span>":"") + "<div class='clear'></div></a>" )
.appendTo( ul );
};
});
</script>
I am using laravel to import these two php files. Nothing fancy there. Just using the link to import those two .php files from my script. Now the q
is my search input field and s
is hidden field. I am using Jquery UI to autocomplete/suggest the json returned data. But The error I am getting is this:
b>Notice</b>: Undefined index: i in <b>C:\xampp\htdocs\proj\public\inc\suggest.php</b> on line <b>22</b><br />
<br />
<b>Notice</b>: Undefined index: s in <b>C:\xampp\htdocs\proj\public\inc\suggest.php</b> on line <b>23</b><br />
<br />
<b>Notice</b>: Undefined index: q in <b>C:\xampp\htdocs\proj\public\inc\suggest.php</b> on line <b>23</b><br />
And a bunch of result (which is lengthy and not pasting).
What seems to be the issue? When I type the word, 'limit' it suggests like it should. And there is no errors. But this error is when I typed 'speed'. There are some words which works fine. And others gives me errors. From this line on suggest.php:
$s[] = array('label' => $d['l'], 'value' => $d['id'], 'cast' => $d['s'], 'img' => $img, 'q' => $d['q']);
I do not understand why this will not work.