【问题标题】:Is it possible to get a URL from a JSON file to display an image from img src是否可以从 JSON 文件中获取 URL 以显示来自 img src 的图像
【发布时间】:2021-09-03 16:35:41
【问题描述】:

(注意:json 新手)我有一个 JSON 文件,其中包含图像 URL (https://pastebin.com/TavfuziF),代码如下:

 <!DOCTYPE html>
<html>
  
<body>
    <center>
<?php 

$jdata = file_get_contents("cats-in-moviesnew.json");
//decode the retrieved json file into an array
$contents = json_decode($jdata, true);

echo
//create the table
"<table style border=3px solid black; ><tr><th>Producer</th><th>Director</th><th>Title</th><th>Year made</th><th>URL</th><th>Image</th><th>Poster URL</th></tr>";


//filling the table
foreach($contents as $record) {
echo
"<tr>
<td>" . $record["fields"]["produced_by"] . "</td>
<td>" . $record["fields"]["directed_by"] . "</td>
<td>" . $record["fields"]["title"] . "</td>
<td>" . $record["fields"]["year"] . "</td>
<td>" . $record["fields"]["url"] . "</td>
<td>" . 

$record["fields"]["url_poster"]

. "</td>
<td>" . $record["fields"]["url_poster"] . "</td>

</tr>";
}
?>
</body>
</center>
  
</html>

我一直在试图弄清楚是否有可能在里面做类似 的事情(目前差距在表格填充位中)代码是否可能。我尝试了几件对我没有用的事情,我看到过一些关于将东西转换为 base64 的事情,但在我的水平上这对我来说非常复杂,我现在不知道还能做什么。任何帮助将不胜感激。

【问题讨论】:

  • 是的,你可以这样做。如果它不起作用,请输出该值以确保它包含您期望的路径。看看你的报价。
  • 请将 json 添加到问题中,而不是作为场外链接,否则当链接过期时,问题对未来的访问者没有帮助。您不需要发布整个内容,只需举几个示例项目来展示格式。
  • 尝试print_r($record); 了解您的数据是什么样的,以及如何访问它。

标签: php html json


【解决方案1】:

你可以这样做:

echo '<table>';
foreach($contents as $record){
    echo '<tr>';
        echo '<td><img src="'.$record['fields']['poster_url'].'" /></td>';
    echo '</tr>';
}
echo '<table';

只是一条建议,在您的代码上使用缩进,以便在有人处理代码的功能中更容易阅读和调试。 :)

【讨论】:

    【解决方案2】:

    没关系,我发现只需要在 $record["fields"]["url_poster"] 之前添加 {},所以它看起来像:"&lt;img src={$record["fields"]["url_poster"]}&gt;"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      相关资源
      最近更新 更多