简体   繁体   English

我如何使用给定的php代码在html页面中显示所有json数据

[英]how can i display all the json data in the html page using the given php code

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. 当我在搜索字段中输入电影名称时,我想显示所有是json文件的电影,但是使用此代码,我只能获得其中一部电影,请您帮助我。

<?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>";



    }

        ?>

you have need to use foreach loop to get all the search result. 您需要使用foreach循环来获取所有搜索结果。

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>";
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 PHP 从 MYSQL 返回数据为 JSON,以便我可以在我的 HTML 页面上使用它 - How can I return data from MYSQL as JSON using PHP so that I can use it in on my HTML page 我可以在JOOMLA自定义HTML模块中放入什么php代码来显示page_heading-使用Sourcerer - What php code can I put in a JOOMLA custom HTML module to display a page_heading - using Sourcerer 我想使用Ajax将json数据显示到html页面中的卡上 - I wanna display json data to a card in html page using ajax 如何在同一 HTML 页面上显示 HTML GET 请求数据,使用单独的 PHP 文件来获取它? - How do I display the HTML GET request data on the same HTML page, using a separate PHP file to fetch it? 如何使用php在html中显示json数据的动态键和值 - How to display dynamic keys and values of json data in html using php 如何使用 php 显示从 mysql 到 html 页面的数据 - How to display data from mysql using php to html page 如何在页面内显示和格式化HTML代码? - How can I display and format HTML code within the page? 我如何在另一个PHP页面中显示所有数组值 - How can I display all the array value in another php page 如何在html页面中显示使用php由gd生成的png图像(饼图)? - How can I display a png image (pie chart) generated by gd using php in my html page? 如何在 html 页面中动态显示 php 代码 - How to display php code in html page dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM