1

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.

BasicBud
  • 55
  • 9
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Qirel Aug 04 '18 at 09:00
  • @Qirel The answer you provided helped but I do not see why I am getting the notice. The variables are initialized and defined. Yet I am still getting the error. – BasicBud Aug 04 '18 at 09:11
  • Dump `$d` inside your loop with `var_dump($d);` - what does it output? Seems like it doesn't always hold the indexes you are attempting to use. – Qirel Aug 04 '18 at 13:21
  • Yes you are right. It does not always have a value in it. And it returns null. That is why it is giving me the error. I potsed my own answer. For anyone who stumbles across a similar issue with an imdb api or such. – BasicBud Aug 04 '18 at 13:28

1 Answers1

0

Ok I now see the problem. The suggestion by @Qirel helped a ton. I missed the value q from my json output. The issue was that some of the results output of q was null and thus, the undefined index.

Laurel
  • 5,965
  • 14
  • 31
  • 57
BasicBud
  • 55
  • 9