0

when i type in a movie name in the search field i want to display all the movies that is the json file but with this code i can only get one of the movies can you please help me with this.

<?php

            if (isset($_POST['submit-search'])) {

                $txtresult = $_POST['search'];

                function    getImdbRecord($title, $ApiKey)
                    {
                        $path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
                        $json = file_get_contents($path);
                        return json_decode($json, TRUE);

                    }
                $data = getImdbRecord($txtresult, "f3d054e8");  


                 echo "<div class = 'info-box'><img src =".$data['Poster']."</img><h3> Name :".$data['Title']."</h3><h3> Year : ".$data['Year']."</h3><h3> Duration : ".$data['Runtime'],"</h3></div>";



    }

        ?>

1 Answer 1

1

you have need to use foreach loop to get all the search result.

like.

$data = getImdbRecord($txtresult, "f3d054e8");  
foreach($data['Search'] as $value){
    echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
}

Complete code will be.

 <?php
 //add function out side the if condition
function getImdbRecord($title, $ApiKey){
                $path = "http://www.omdbapi.com/?s=$title&apikey=$ApiKey";
                $json = file_get_contents($path);
                return json_decode($json, TRUE);

}


if (isset($_POST['submit-search'])) {
   $txtresult = $_POST['search'];
   $data = getImdbRecord($txtresult, "f3d054e8");  
   //use loop to get all the seacrh result.
    foreach($data['Search'] as $value){
        echo "<div class = 'info-box'><img src =".$value['Poster']."</img><h3> Name :".$value['Title']."</h3><h3> Year : ".$value['Year']."</h3><h3> Duration : ".$value['Runtime'],"</h3></div>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.