mcchickennug
mcchickennug

Reputation: 11

Is it possible to get a URL from a JSON file to display an image from img src

(note: new to json) I have a JSON file that contains image URLs inside of it (https://pastebin.com/TavfuziF), with the following code:

 <!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>

and Ive been trying to figure out if its possible to do something like <img src="$record["fields"]["url_poster"]"> (currently where the gap is in the table filling bit) inside that code is possible or not. Ive tried several things which haven't worked for me, Ive seen things talking about converting stuff to base64 but it's very complicated for me at my level, and I don't know what else to do at this point. Any help would be greatly appreciated.

Upvotes: 0

Views: 138

Answers (2)

cbrr09
cbrr09

Reputation: 179

You can do it like this:

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

and just a piece of advice, use indentation on you code so in the feature someone handle the code it is more easier to read and easier to debug. :)

Upvotes: 1

mcchickennug
mcchickennug

Reputation: 11

nevermind I figured it out just needed to add {} before the $record["fields"]["url_poster"], so it looks like: "<img src={$record["fields"]["url_poster"]}>"

Upvotes: 1

Related Questions