因此,我正在尝试使用IMDB建议创建搜索栏,这是到目前为止的目标。这是我的Suggest.php文件。
<?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
}
?>
这是我的imdbImage.php文件:
<?php
header("Content-type: image/jpeg");
//URL for IMDb Image.
$url = rawurldecode($_REQUEST['url']);
echo file_get_contents($url);
?>
最后是脚本:
<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>
我正在使用laravel导入这两个php文件。没什么可看的。只需使用链接从我的脚本中导入这两个.php文件。现在q
是我的搜索输入字段,而s
是隐藏字段。我正在使用Jquery UI自动完成/建议json返回的数据。但是我得到的错误是:
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 />
还有一堆结果(冗长而不粘贴)。
似乎是什么问题?当我输入单词“ limit”时,它表明它应该是应该的。而且没有错误。但这是我输入“速度”时出现的错误。有些话效果很好。其他人给我错误。从上这行对suggest.php:
$s[] = array('label' => $d['l'], 'value' => $d['id'], 'cast' => $d['s'], 'img' => $img, 'q' => $d['q']);
我不明白为什么这行不通。
答案 0 :(得分:0)
好,我现在看到了问题。 @Quirel的建议帮助了很多人。我从json输出中错过了值q
。问题是q
的某些结果输出为null,因此未定义索引。