Wayne Allen Wayne Allen - 3 months ago 9
CSS Question

Assign a style if php array containts value

Hi Im using tmdb api and fetching a actors movies list var json. the code works how I want it to apart from I am trying to style the movie thumbnail differently to the rest if the film is a horror.

$tmdb = $_GET['tmdb'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/person/".$tmdb."?api_key=#######");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$person = curl_exec($ch);
curl_close($ch);
$actor= json_decode($person, true); // Decode the results into an array
$name = $actor['name'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/discover/movie?&with_cast=".$tmdb."&vote_count.gte=5&sort_by=release_date.desc&api_key=#######");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$personCredits = curl_exec($ch);
curl_close($ch);
$credits= json_decode($personCredits, true); // Decode the results into an array

$counter4 = 0;
$counter5 = 0;
echo "<pre>";
print_r($credits);
echo "</pre>";

$page=1;
echo "<div style='width:800px;overflow:hidden;position:absolute;ktop:1100px;left:75'>";

while ( $page <= $credits['total_pages'] ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/discover/movie?&with_cast=".$tmdb."&vote_count.gte=5&sort_by=release_date.desc&page=".$page."&api_key=#######");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
$personCredits = curl_exec($ch);
curl_close($ch);
$credits= json_decode($personCredits, true); // Decode the results into an array

$resultsCount4=(count($credits['results']))-1;
while ( $counter4 <= $resultsCount4 ) {

// THIS IS WHERE I NEED HELP PLEASE>>>>>
if($credits['results'][$counter4]['genre_ids'] == 27) {
$style="horror";
} else { $style="round"; }
////////////////////////////////////////////////////

echo "<div style='width:200;height:260;float:left;text-overflow:ellipsis white-space: nowrap;
width: 14em;
overflow: hidden;
'><center><div class='".$style."'><a href=\"/?id=moviepage.php&ttno=".$credits['results'][$counter4]['id']."\"><img width=\"154\" height=\"231\"onerror=\"this.src='nocover.png'\" src='http://image.tmdb.org/t/p/w154/".$credits['results'][$counter4]['poster_path']."'></div><br>".$credits['results'][$counter4]['title']."<br>".$credits['results'][$counter4]['release_date']."</center></a></div>";
$counter4++;
}
$page++;
$counter4=0;
}


the Json array looks like

Array
(
[page] => 1
[results] => Array
(
[0] => Array
(
[poster_path] => /ceVMgY3TzLrfEpaMfaOnPDYnfqA.jpg
[adult] =>
[overview] => John Wick is forced out of retirement by a former associate looking to seize control of a shadowy international assassins’ guild Bound by a blood oath to aid him, Wick travels to Rome and does battle against some of the world’s most dangerous killers.

[release_date] => 2017-02-10
[genre_ids] => Array
(
[0] => 28
)

[id] => 324552
[original_title] => John Wick: Chapter Two
[original_language] => en
[title] => John Wick: Chapter Two
[backdrop_path] => /6TPIMjoyRKCKhCGeGigP99qQTWw.jpg
[popularity] => 1.714191
[vote_count] => 7
[video] =>
[vote_average] => 9.57
)

[1] => Array
(
[poster_path] => /l9Eu1e3qNvFSvi66WtHFBoHIgeT.jpg
[adult] =>
[overview] => A defense attorney works to get his teenage client acquitted of murdering his wealthy father.
[release_date] => 2016-06-10
[genre_ids] => Array
(
[0] => 18
[1] => 53
)

[id] => 331583
[original_title] => The Whole Truth
[original_language] => en
[title] => The Whole Truth
[backdrop_path] => /1pwF12CCtUSmCifDGhZHohp5qYu.jpg
[popularity] => 1.875005
[vote_count] => 6
[video] =>
[vote_average] => 6.67
)


Thanks in advance

Answer

genre_ids is an array (notice that the name is plural, and "The Whole Truth" is in two genres), not a number, so

if($credits['results'][$counter4]['genre_ids'] == 27)

won't work. Use in_array()

if (in_array(27, $credits['results'][$counter4]['genre_ids'])