I’ve got this bit of php here that is very sluggish. Not millisecond sluggish, but causes the page to load in 8 seconds sluggish.
What it is doing is pulling Actor and Director information from a 3rd party movie database with a json string, for each of the 20 movies on the page.
I’ve done some testing and worked out that my code here must be fine. It’s the connecting to the database 20 times that is the problem.
So other than reducing the amount of movies on the page from 20, is there anything else I could do?
echo '<h3>Starring</h3>
<p>';
$num_actors = 0;
$films_result = $tmdb->getMovie($film->id);
$films = json_decode($films_result);
foreach ($films as $film) {
foreach ($film->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 ($film->cast as $cast) {
if ($cast->job == 'Director') {
echo '<a href="person.php?id=' . $cast->id . '">' . $cast->name . '</a> ';
}
}
echo '</p>';
I don’t know if it will help, but here’s the database’s documentation page on the call I am making to it – http://api.themoviedb.org/2.1/methods/Movie.getInfo
Is it safe to assume getMovie will only return results for 1 movie? if so you can rid yourself of the foreach $films loop but we’ll leave that for now.
don’t loop twice – if you find the director then move onto the next loop…
this script will only show a mximum of 5 actors – beware of that it may not be what you intended