开发者

Nested foreach statements are invalid. Help?

开发者 https://www.devze.com 2023-03-04 04:23 出处:网络
Please forgive the extreme-beginner style of coding. I\'m working with PHP and JSON strings from an API for the first time.

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 开发者_Go百科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:

Nested foreach statements are invalid. Help?

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消