【问题标题】:how can i display all the json data in the html page using the given php code如何使用给定的 php 代码在 html 页面中显示所有 json 数据
【发布时间】:2019-07-21 11:55:59
【问题描述】:

当我在搜索字段中输入电影名称时,我想显示所有 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>";



    }

        ?>

【问题讨论】:

    标签: php html json


    【解决方案1】:

    您需要使用 foreach 循环来获取所有搜索结果。

    喜欢。

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

    完整的代码将是。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2019-09-30
      相关资源
      最近更新 更多