错误的数组?坏的foreach? [英] Bad array? Bad foreach?
问题描述
对不起,关于这个专题的巨大的无知,但我真的不知道去哪里找比其他这个网站的时候我接触到我的PHP的麻烦。
Sorry for the huge ignorance on the topic, but I really have no idea where to look other than this website when I come into trouble with my PHP.
我想在这里做的是使用pre指定的ID从数据库中调用特定的电影。但我得到的是一个'()提供的foreach无效参数在第二和第三的foreach的下方。
What I'm trying to do here is use pre-designated IDs to call particular movies from a database. But all I get is an 'Invalid argument supplied for foreach()' message on the second and third foreach's below.
下面是在我的头上code:
Here's my code in the head:
//Custom lists of movies to bring in
//New Releases list
$films_new_releases = array(40805, 46705, 41630, 44564, 39451, 20352, 43933, 49009, 49797, 42194);
//Most Popular list
$films_most_popular = array(27205, 16290, 10138, 41733, 37799, 18785, 19995, 17654, 10140, 12162);
//Get information from address bar
$list = $_GET['l'];
if ($list == 'new releases') {
$list_chosen = $films_new_releases;
}
elseif ($list == 'most popular') {
$list_chosen = $films_most_popular;
}
else {
$list_chosen = $films_new_releases;
}
和中跻身正文:
// Loop through each film returned
foreach ($list_chosen as $list_chosen_film) {
$films_result = $tmdb->getMovie($list_chosen_film);
$film = json_decode($films_result);
// 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 == 'poster') {
$backdrop_url = $backdrop->image->url;
}
}
echo '<div class="view-films-film">
<a href="film.php?id=' . $film->id . '"><img src="' . $backdrop_url . '" alt="' . $film->name . '" /></a>
<div class="view-films-film-snippet">
<h2><a href="film.php?id=' . $film->id . '">' . $film->name . '</a></h2>';
if ($film->certification != null) {
echo '<img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />';
}
echo ' <h3>Starring</h3>
<p>';
$num_actors = 0;
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>
</div>
</div>';
}
// End films
}
小测试中,我所做的就是检查什么 $ list_chosen
, $ list_chosen_film
, $ films_result
和 $电影
实际上通过在页面的底部打印它们包含的内容。
The little testing I've done is checking what $list_chosen
, $list_chosen_film
, $films_result
and $film
actually contain by printing them at the bottom of the page.
$ list_chosen
显示 - 阵列
, $ list_chosen_film
节目 - 42194
, $ films_result
显示整个JSON字符串, $电影
显示 - 阵列
$list_chosen
shows - Array
, $list_chosen_film
shows - 42194
, $films_result
shows the entire JSON string, $film
shows - Array
.
推荐答案
尝试添加:
print_r($film->backdrop);
第二前的foreach()循环。该错误消息之前它不会是一个数组或它将包含零个元素(不允许)。如果您还补充:
before the second foreach() loop. Before the error message it won't be an array or it will contain zero elements (not allowed). If you also add:
echo $films_result;
您将能够调试并完全理解什么是错的。如果没有,张贴在你的问题,整个输出。
you will be able to debug it and fully understand what is wrong. If not, post the whole output in your question.
这篇关于错误的数组?坏的foreach?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!