php - Nested foreach statements are invalid. Help? -


please forgive extreme-beginner style of coding. i'm working php , json strings api first time.

what i'm trying here, doesn't work if through it, print out multiple movie results user searching for. search results page - movie poster , key details next each item.

one of things want next each result list of first 5 actors listed in movie, "starring" , directors listed in movie "director".

the problem no doubt fact i've nested foreach statements. don't know other way of doing this.

please please simplest answer best me. don't mind if it's bad practice, whatever solves quickest perfect!

here's code:

  // check if there results   if ($films_result == '["nothing found."]' || $films_result == null) {   }   else {         // loop through each film returned       foreach ($films $film) {          // set default poster image use if film doesn't have 1         $backdrop_url = 'images/placeholder-film.gif';          // loop through each poster current film         foreach($film->backdrops $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 $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 $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 result this:

in situ

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

why using $films[0] when you're in foreach ($films $film)? should using $film->cast right? , should dump $film @ start of loop var_dump , inspect - make sure $cast array , populated. if it's not, work there.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -