Pergunta

I want to run a search for everything under a give title name for example star wars like this search here http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=star%20wars

I want to list all the results in a table format

here is the code i have so far i have had to change from using the easy omdb api because that will only allow up to ten results

right now i keep getting javascript errors any help plz i am aware i need to set up a localproxy NEED HELP PLZ

Would Love Examples

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Sample</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script>
    $( document ).ready(function() {
        $("#SampleSearchButton").click(function() {
            getImdbInfo($("#title").val());
        })
    });

    // The function below takes the entered title and searchs imdb for a match then it displays as followed

    function getImdbInfo(Title) {
        $.ajax({
          url: "http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=" + Title,
          cache: false,
          dataType: "json",
          success: function(data) {
                // you get an object for iteration, the keys are Title, Type, Year, imdbID
                console.log(data);

                var str = '<table>';
                str += "<thead><th>Index</th><th>Title</th><th>Type</th><th>Year</th><th>imdbID</th></thead>"

                // iterate over the data result set
                $.each(data.Search, function(index, element) {
                    str += "<tr>";
                    str += "<td>" + index + "</td>";
                    str += "<td>" + element.Title + "</td>";
                    str += "<td>" + element.Type + "</td>";
                    str += "<td>" + element.Year + "</td>";
                    str += "<td>" + element.imdbID + "</td>";
                    str += "</tr>";
                });

                str += '</table>';

                // insert the html
                $("#SampleResults").html(str);
          },
          error: function (request, status, error) { alert(status + ", " + error); }
        });
    }
    </script>
</head>
<body>


<!-- search textbox -->
<input type="text" id="title" placeholder="Enter Name for search">

<!-- do search button -->
<button type="text" id="SampleSearchButton">Search</button>

<!-- display results container -->
<div id="SampleResults"></div>
</body>
</html>
Foi útil?

Solução

  • you enter the title
  • the title is append to a url, which calls your local php file
  • the local php file accepts the title and attaches it the API url you want to call
  • the request is made and the content is returned
  • returned content is then accepted by js
  • check console.log for exact data structure
  • to main keys "title_popular" and "title_exact", that's why there are two tables
  • watch out for "description" and "title_description", both seem to be the same (API BUG?), so they are printed twice!
  • i don't have the time to structure the table fully
  • maybe you should ask someone, how to print the multilevel object more elegant

PHP

imdb-fetcher.php

 <?php
 $title = $_GET['title']; // <- you need to secure this
 echo file_get_contents(
    'http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=' . $title
 );

HTML

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Sample</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script>
    $( document ).ready(function() {
        $("#SampleSearchButton").click(function() {
            getImdbInfo($("#title").val());
        })
    });

    // The function below takes the entered title and searchs imdb for a match then it displays as followed

    function getImdbInfo(title) {
         $.ajax({
          type: 'GET',
          url: "imdb-fetcher.php?title=" + title, // <-- request to PHP data fetcher
          dataType: "json",
          success: function(data) {
                // you get an object for iteration, see console for keys
                console.log(data);

                // table for title_popular
                var str = '<table>';
                str += "<thead><th>Index</th><th>id</th><th>title</th><th>title_desc</th><th>year</th><th>description</th></thead>";

                $.each(data.title_popular, function(index, element) {
                    str += "<tr>";
                    str += "<td>" + index + "</td>";
                    $.each(element, function(key, element) {
                        str += "<td>" + element + "</td>";
                    });
                    str += "</tr>";
                });

                str += '</table>';

                // table for title_exact
                str += '<table>';
                str += "<thead><th>Index</th><th>id</th><th>title</th><th>title_desc</th><th>year</th><th>description</th></thead>";

                $.each(data.title_exact, function(index, element) {
                    str += "<tr>";
                    str += "<td>" + index + "</td>";
                    $.each(element, function(key, element) {
                        str += "<td>" + element + "</td>";
                    });
                    str += "</tr>";
                });


                // insert the html
                $("#SampleResults").html(str);
          },
          error: function (request, status, error) { alert(status + " - " + error); }
        });
    }
    </script>
</head>
<body>


<!-- search textbox -->
<input type="text" id="title" placeholder="Enter Name for search">

<!-- do search button -->
<button type="text" id="SampleSearchButton">Search</button>

<!-- display results container -->
<div id="SampleResults"></div>
</body>
</html>

Result

enter image description here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top