Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested foreach statements are invalid. Help?

Tags:

json

php

Please forgive the extreme-beginner style of coding. I'm working with PHP and JSON strings from an API for the first time.

What I'm trying to do here, which obviously doesn't work if you look through it, is print out multiple movie results that the user is searching for. This is a search results page - with a movie poster and some key details next to each item.

One of the things I want next to each result is a list of the first 5 actors listed in the movie, as "Starring" and then any directors listed in the movie as "Director".

The problem is no doubt the fact that I've nested foreach statements. But I don't know any other way of doing this.

Please please the simplest answer would be the best for me. I don't mind if it's bad practice, just whatever solves it quickest would be perfect!

Here's the code:

  // Check if there were any results
  if ($films_result == '["Nothing found."]' || $films_result == null) {
  }
  else {  
      // Loop through each film returned
      foreach ($films as $film) {

        // Set default poster image to use if film doesn't have one
        $backdrop_url = 'images/placeholder-film.gif';

        // Loop through each poster for current film
        foreach($film->backdrops as $backdrop) {
          if ($backdrop->image->size == 'thumb') {
            $backdrop_url = $backdrop->image->url;
          }
        }
        echo '<div class="view-films-film">
            <a href="film.php?id=' . $film->id . '"><img src="' . $backdrop_url . '" alt="' . strtolower($film->name) . '" /></a>
                <div class="view-films-film-snippet">
                    <h2><a href="film.php?id=' . $film->id . '">' . strtolower($film->name) . '</a></h2>
                    <img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />
                    <h3>Starring</h3>
                    <p>';
        $num_actors = 0;
        foreach ($films[0]->cast as $cast) {
        if ($cast->job == 'Actor') {
          echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
          $num_actors++;
          if ($num_actors == 5)
            break;
        }
        }
        echo '      </p>
                    <h3>Director</h3>
                    <p>';
        foreach ($films[0]->cast as $cast) {
            if ($cast->job == 'Director') {
                echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
            }
        }
        echo '      </p>
                </div>
            </div>';

      // End films
      }
  }

An example of the result would be this:

in situ

with line 120 being foreach ($films[0]->cast as $cast) { and line 131 being foreach ($films[0]->cast as $cast) {

like image 471
Rowan Avatar asked May 25 '26 07:05

Rowan


1 Answers

Why are you using $films[0] when you're in a foreach ($films as $film)? You should be using $film->cast right? And you should dump $film at the start of your loop with var_dump and inspect it - make sure $cast is an array and is populated. If it's not, work back from there.

like image 130
David Fells Avatar answered May 26 '26 20:05

David Fells



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!