0

The foreach function is not running on this JSON file.

I would like the PHP to loop through the JSON file and output the data in an HTML table format.

<?php
// Store JSON data in a PHP variable
$url = 'https://eztv.io/api/get-torrents?limit=100&page=1'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
//var_dump(json_decode($data));
$shows = json_decode($data, true); // decode the JSON feed and make an associative array
?>
<br /><br /><br /><br />

<table>
<?php
foreach ($shows as $shows) : ?>
    <tr>
        <td> <strong><?php echo $shows["title"] . "\n"; ?></strong>  </td>
        <td> <strong><?php echo $shows["season"] . "\n"; ?></strong>  </td>
        <td> <strong><?php echo $shows["episode"] . "\n"; ?></strong>  </td>
        <td> <a href="<?php echo $shows["magnet_url"] . "\n"; ?>">magnet</a></td>
        <td> <?php echo $shows["date_released_unix"] . "\n"; ?>  </td>
        <td> <?php echo $shows["size_bytes"] . "\n"; ?>  </td>
        <td> <a href="<?php echo $shows["episode_url"] . "\n"; ?>">episode Link</a></td>
        <td> <?php echo $shows["imdb_id"] . "\n"; ?>  </td>
    </tr>
<?php endforeach; ?>
</table>

If I run this code I get a Notice: Undefined index: error on the page.

3
  • 1
    we do not know how the json string is Commented Jul 31, 2019 at 10:03
  • The link to the json is eztv.io/api/get-torrents?limit=100&page=1 or should i post it here ? Commented Jul 31, 2019 at 10:10
  • Do not use link, post the json file (if its huge post the part we need to read to help you) Commented Jul 31, 2019 at 10:31

2 Answers 2

1

All the data at torrents key of response . You should check the array index/key exist or not.

<?php 
$url = 'https://eztv.io/api/get-torrents?limit=100&page=1'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$shows = json_decode($data, true); // decode the JSON feed and make an associative array 
<?php foreach ($shows['torrents'] as $shows) : ?>
    <tr></tr>
<?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

Comments

0
foreach ($shows as $shows) : ?>
    <tr>
        <td> <strong><?php echo $shows["title"] ?? '' . "\n"; ?></strong>  </td>
        <td> <strong><?php echo $shows["season"] ?? '' . "\n"; ?></strong>  </td>
        <td> <strong><?php echo $shows["episode"] ?? '' . "\n"; ?></strong>  </td>
        <td> <a href="<?php echo $shows["magnet_url"] ?? '' . "\n"; ?>">magnet</a></td>
        <td> <?php echo $shows["date_released_unix"] ?? '' . "\n"; ?>  </td>
        <td> <?php echo $shows["size_bytes"] ?? '' . "\n"; ?>  </td>
        <td> <a href="<?php echo $shows["episode_url"] ?? '' . "\n"; ?>">episode Link</a></td>
        <td> <?php echo $shows["imdb_id"] ?? '' . "\n"; ?>  </td>
    </tr>
<?php endforeach; ?>

Similar to Shivendra Singh's answer but instead we use a null coalescent just to make things easier to read.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.