2011-05-16 39 views
0

对不起,我对这个话题一无所知,但当我遇到我的PHP出现问题时,我真的不知道该去哪里看看这个网站。坏阵列?坏的foreach?

我想在这里做的是使用预先指定的ID从数据库调用特定的电影。但是我所得到的是在下面的第二和第三个foreach中为'foreach()'提供的'无效参数'。

这里是我的头代码:

//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$film实际上包含通过将它们打印在页面的底部。

$list_chosen节目 - Array$list_chosen_film节目 - 42194$films_result显示整个JSON字符串,$film节目 - Array

+0

结果是什么:var_dump($ film)? – haknick 2011-05-16 01:04:23

+0

这里是var_dump($ film)结果的开始: 'array(1){[0] => object(stdClass)#249(33){[“popular”] => int(3)[“translation “] =>布尔(真)......' 我会发布整个,但它太大了 – Rowan 2011-05-16 16:25:35

+0

为什么不是这篇文章得到关注,那么,如果发布它的确切副本呢?哪一个关闭? – Rowan 2011-05-16 22:17:22

回答

1

发生这种情况是因为 - 由于PHP显示的错误通知了您 - 您提供了错误的参数给foreach循环,可能是null或其他值。确保你提供了数组来foreach。

此外,每次使用foreach时间,那样做:

if (count($some_list) > 0) { 
    foreach ($some_list as $list_item) { 
     // code for each item on the list 
    } 
} else { 
    // code when there is nothing on the list 
} 

这将确保你不会看到错误,只是因为没有在名单上。

编辑:

documentation page,你可以找到一些小费如何避免这样的错误,如果你想通过遍历集合为空。只需将该集合投射到array类型:

foreach ((array) $some_list as $list_item) { 
    // code for each item on the list 
} 
0

您能否提供$ film的转储?该错误告诉你,你正在指向一个无法迭代的对象(最有可能为空)。

+0

var_dump($ film)的结果如下: 'array(1){[0] => object(stdClass)#249(33){[“popular”] => int(3)[“translation “] =>布尔(真)......' 我会发布整个很多,但它太大了 – Rowan 2011-05-16 16:25:47

+0

转储$电影 - http://freetexthost.com/4cfneqdqty – Rowan 2011-05-17 00:15:46

2

尝试增加:

print_r($film->backdrop); 

第二foreach()循环之前。错误消息之前它不会是一个数组,或者它将包含零个元素(不允许)。如果您还添加:

echo $films_result; 

您将能够调试它并完全理解错误。如果不是,请在问题中发布整个输出。

+0

我做了'print_r ($电影 - >背景);'和它什么也不印。我为'$ list_chosen','$ list_chosen_film','$ films_result','$ film'完成了相同的操作,并且它们看起来都很完美......在这里我保存了整个'$ film'输出 - > http: //freetexthost.com/4cfneqdqty。我看起来很好吗?背景在那里和每个人 – Rowan 2011-05-17 00:14:49