-1

(note: new to json language) I have this bit of code that's supposed to get data from a json file and display it in a table with a set of data on the topic of movies on each row, but the code isn't working and comes up with error "Unidentified index: produced_by on line 20" or directed_by on line 21, and so on for all 7 different data names.

   <?php 

$jdata = file_get_contents("cats-in-movies.json");
//decode the retrieved json file into an array
$contents = json_decode($jdata, true);
//how long is the array
$i = count($contents);
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>";
//from 0 to length_of_array show the values in the table
for ($x = 0; $x < $i; $x++) {
echo
"<tr>
<td>" . $contents[$x]["produced_by"] . "</td>
<td>" . $contents[$x]["directed_by"] . "</td>
<td>" . $contents[$x]["title"] . "</td>
<td>" . $contents[$x]["year"] . "</td>
<td>" . $contents[$x]["url"] . "</td>
<td>" . $contents[$x]["image"] . "</td>
<td>" . $contents[$x]["url_poster"] . "</td>

</tr>";
}
?>

Its really basic code used from elsewhere that worked with a variable of $jdata being actual json written inside the file instead of file_get_contents, but now with a file it doesn't seem to work, and Im trying to let the json file be updated so more data can be added in somewhat easily so just copying the text into the php file won't work for me.

The actual JSON file is here: https://pastebin.com/TavfuziF

Ive tried for several hours now and I can't find anything anywhere on how to fix this so any help would be greatly appreciated.

  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – B001ᛦ Jun 17 '21 at 10:43

4 Answers4

0

Just don't access the index, when it doesn't exist; for example:

isset($contents[$x]["produced_by"]) ? $contents[$x]["produced_by"] : '&nbsp;'
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
0

Your structure is different... thats why it doesn't work.

Single record contains for example:

 Array
        (
            [datasetid] => cats-in-movies
            [recordid] => 043bb3668780c988b07e5c5811c65500d5006163
            [fields] => Array
                (
                    [produced_by] => Walt Disney
                    [url] => https://en.wikipedia.org/wiki/Lady_and_the_Tramp
                    [image] => Array
                        (
                            [mimetype] => image/jpeg
                            [format] => JPEG
                            [color_summary] => Array
                                (
                                    [0] => rgba(234, 220, 53, 1.00)
                                    [1] => rgba(202, 180, 115, 1.00)
                                    [2] => rgba(213, 192, 122, 1.00)
                                )

                            [filename] => lady_and_the_tramp.jpg
                            [width] => 220
                            [id] => f3f58bbd1022f26aa1d81a56a926f42e
                            [height] => 329
                            [thumbnail] => 1
                        )

                    [year] => 1955
                    [url_poster] => https://upload.wikimedia.org/wikipedia/en/th                                                                                                                                                             umb/3/39/Lady-and-tramp-1955-poster.jpg/220px-Lady-and-tramp-1955-poster.jpg
                    [directed_by] => Clyde Geronimi, Wilfred Jackson, Hamilton L                                                                                                                                                             uske
                    [title] => Lady and the Tramp
                )

            [record_timestamp] => 2018-11-23T03:50:35.850+10:00
        )

)

So...

For example $contents[$x]["produced_by"] should be $contents[$x]["fields"]["produced_by"].

And image is an array of attributes, you cant just echo it.

Btw. you could use:

foreach{$contents as $record} {
    // $record is like $contents[$x] from your code, for example:
    echo $record["fields"]["produced_by"];
}

instead of for.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91
  • Thanks but how would you echo image inside the table? Sorry Im really new to json. – mcchickennug Jun 17 '21 at 10:58
  • I have no idea where do you get that data from, so I don't know. JSON doesn't contain the image itself. It is hosted somewhere. Where? I have no idea - you should know it. To display it on the table you need to put it on `` tag. For example `` ... there is some additional query to the api to get the image url ... for example `https://en.wikipedia.org/w/api.php?action=query&titles=File:Test.jpg&prop=imageinfo&iilimit=50&iiend=2007-12-31T23:59:59Z&iiprop=timestamp|user|url` . Read the documentation! – Flash Thunder Jun 17 '21 at 11:02
0

I stored your JSON on JSONKEEPER . Following will generate your desired table:

$data = file_get_contents("https://jsonkeeper.com/b/192C");
$contents = json_decode($data, true);


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

foreach ($contents as $key => $content) {
    echo "<tr>";
        echo "<td>".$content['fields']['produced_by']."</td>";
        echo "<td>".$content['fields']['directed_by']."</td>";
        echo "<td>".$content['fields']['title']."</td>";
        echo "<td>".$content['fields']['year']."</td>";
        echo "<td>".$content['fields']['url']."</td>";
        echo "<td>".$content['fields']['image']['filename']."</td>";
        echo "<td>".$content['fields']['url_poster']."</td>";
    echo "</tr>";
}

echo "</table>";

enter image description here

Raju Rayhan
  • 101
  • 1
  • 8
-1

use

$contents[$x]["fields"]["directed_by"]

instead of

$contents[$x]["directed_by"]

I think you're confused by json structure.

Denis Turgenev
  • 138
  • 1
  • 9