【问题标题】:foreach only bringing back last item in arrayforeach 只带回数组中的最后一项
【发布时间】:2013-03-15 10:05:17
【问题描述】:

我正在尝试根据来自两个不同表的信息从 API 中提取数据,然后通过 foreach 循环并将这些数据解析到查询中。我已经构建了一系列查询,可以得到我正在寻找的确切数据(数组中第一部电影的名称和海报),但是当将此数据解析到返回一系列电影列表的 foreach 中时,我预定义的变量只会被带回数组中的最终列表,而不是像预期的那样循环遍历它们。

这是我目前得到的:

处理:

// Get a user's Watchlists from the watchlists table
    $query = "SELECT * FROM watchlists WHERE user_id = " . $profile_info['id']; 
    $watchlist_result = mysql_query($query);
    $watchlists = array();
    while(($row = mysql_fetch_assoc($watchlist_result))) {
        $watchlists[] = $row;
    }

    // Count how many Watchlists a user has
    $watchlist_count = count($watchlists);

    // Echo details of each Watchlist
    echo "<pre>";
    print_r($watchlists);
    echo "</pre>";

    // For each Watchlist, select all of the films it contains and echo them out
    foreach ($watchlists as $key => $films) {
        // Get each Watchlist's ID from the watchlists table
        $watchlist_id = $films['watchlist_id'];
        echo "<pre>";
        print_r($watchlist_id);
        echo "</pre>";

        // Extract films from the watchlist_films table for each Watchlist, based on its unique ID and put them into an array
        $watchlist_film_query = "SELECT * FROM watchlist_films WHERE watchlist_id = " . $watchlist_id;
        $watchlist_film_result = mysql_query($watchlist_film_query);
        $watchlist_films = array();
        while(($row = mysql_fetch_assoc($watchlist_film_result))) {
            $watchlist_films[] = $row;
        }

        // Echo the new array
        echo "<pre>";
        print_r($watchlist_films);
        echo "</pre>";

        // Get the ID of the first film in each Watchlist
        $rt_id = $watchlist_films[0]['film_id'];

        // Echo the ID
        echo "<pre>";
        print_r($rt_id);
        echo "</pre>";

        // Initialise Rotten Tomates API
        include_once('/api/RottenTomatoes.php');

        /* Rotten Tomatoes */
        $rottenTomatoes = new RottenTomatoes('2b2cqfxyazbbmj55bq4uhebs', 10, 'uk');

        // Search Rotten Tomatoes for details on the first film in the array
        $rottenTomatoes->movieSearch($rt_id);

        //echo "<pre>";
        try {
            $result = $rottenTomatoes->getMovieInfo($rt_id);
            //print_r($result);
        } catch (Exception $e) {
            //print_r($e);
        }
        //echo "</pre>";

        // Get poster and name of first movie in array
        $thumbnail = $result['posters']['thumbnail'];
        $first_film_name = $result['title'];

        echo "<pre>";
        print_r($thumbnail);
        echo "</pre>";

        echo "<pre>";
        print_r($first_film_name);
        echo "</pre>";
    }

现在,我想将此信息解析为实际页面,并打印出用户拥有的每个关注列表的所有信息。

输出:

if ($watchlist_count != 0) {?>
                        <ul class="unstyled"><?php
                            foreach($watchlists as $key => $watchlist_item) {?>
                                <li class="well list-item clearfix"><?php

                                    echo "<pre>";
                                    print_r($watchlist_item);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($watchlist_films);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($watchlist_id);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($thumbnail);
                                    echo "</pre>";

                                    echo "<pre>";
                                    print_r($first_film_name);
                                    echo "</pre>";?>

                                    <div class="row-fluid">
                                        <a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist">
                                            <img src="<?php echo $thumbnail; ?>" class="span1 pull-left" alt="<?php echo $first_film_name; ?> poster" title="<?php echo $first_film_name; ?> poster" />
                                        </a>

                                        <div class="span11 movie-info">
                                            <p class="search-title"><a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist"><?php echo $watchlist_item['name']; ?></a></p>

                                            <p class="search-synopsis"><?php echo $watchlist_item['description']; ?></p>
                                        </div>
                                    </div>

                                </li><?php
                            }?>
                        </ul><?php
                    } else {?>
                        <div class="well list-item">
                            You haven't created any Watchlists yet! Why not visit a movie page now and build your first?
                        </div><?php
                    }?>

我遇到的问题是,虽然处理部分得到了我想要的东西(即每个监视列表中第一部电影的名称和缩略图),但当我将此信息解析到输出中时,只有最后一个Watchlist中第一部电影的名称和海报显示,如下:

【问题讨论】:

  • 看起来'watchlist_item'既用作数组又用作值。
  • 您确定$profile_info['id'] 只能包含数字吗?另请注意,mysql_* 函数已被弃用(请参阅red box)。
  • @MarcelKorpel 是的,个人资料只能通过其个人资料 ID 作为唯一标识符来标识,该标识符始终是一个数字,监视列表也是如此 - 每个监视列表和每个用户都有一个唯一 ID,并且这两个可以相互关联,即每个唯一用户可以有许多监视列表,每个监视列表都有自己的唯一 ID。

标签: php arrays database foreach rotten-tomatoes


【解决方案1】:

在处理过程中,您会在 foreach 的每个循环中覆盖 $watchlist_films$thumbnail$first_film_name,因此您只能获取最后一次运行的数据。

您应该将数据添加回主数组,例如在 foreach 循环结束时,例如:

$films['watchlist_films']=$watchlist_films;
$films['thumbnail']=$thumbnail;
$films['first_film_name']=$first_film_name;

然后您可以在输出中访问数据,例如:

$watchlist_item['thumbnail'];
$watchlist_item['first_film_name'];

所以你的处理可能看起来像这样(缩短):

$query = "SELECT * FROM watchlists WHERE user_id = " . $profile_info['id']; 
$watchlist_result = mysql_query($query);
$watchlists = array();
while(($row = mysql_fetch_assoc($watchlist_result))) {
    $watchlists[] = $row;
}

// For each Watchlist, select all of the films it contains and echo them out
foreach ($watchlists as $key => $films) {
    $watchlist_film_query = "SELECT * FROM watchlist_films WHERE watchlist_id = " . $films['watchlist_id'];
    $watchlist_film_result = mysql_query($watchlist_film_query);
    $watchlist_films = array();

    //You don't need to load all data if you only want the first one:)
    $row = mysql_fetch_assoc($watchlist_film_result)
    $rt_id = $row['film_id'];

    // Initialise Rotten Tomates API
    include_once('/api/RottenTomatoes.php');

    /////Rotten Tomato Call /////
    //===> Rotten Tomatoe API call code goes here <=== //

    //You don't really need the whole films list
    //$films['watchlist_films']=$watchlist_films;
    $films['thumbnail']=$result['posters']['thumbnail'];
    $films['first_film_name']=$result['title'];
}

然后输出:

if (count($watchlists)> 0) {?>
                    <ul class="unstyled"><?php
                        foreach($watchlists as $key => $watchlist_item) {?>
                            <li class="well list-item clearfix"><?php

                                <div class="row-fluid">
                                    <a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist">
                                        <img src="<?php echo $watchlist_item['thumbnail']; ?>" class="span1 pull-left" alt="<?php echo $first_film_name; ?> poster" title="<?php echo $watchlist_item['first_film_name']; ?> poster" />
                                    </a>

                                    <div class="span11 movie-info">
                                        <p class="search-title"><a href="watchlist.php?id=<?php echo $watchlist_item['watchlist_id']; ?>" title="<?php echo $watchlist_item['name']; ?> Watchlist"><?php echo $watchlist_item['name']; ?></a></p>

                                        <p class="search-synopsis"><?php echo $watchlist_item['description']; ?></p>
                                    </div>
                                </div>

                            </li><?php
                        }?>
                    </ul><?php
                } else {?>
                    <div class="well list-item">
                        You haven't created any Watchlists yet! Why not visit a movie page now and build your first?
                    </div><?php
                }?>

所有未测试;-)

【讨论】:

  • 谢谢,我明白你的意思了。最好的地方是呼应这些变量以获取每部电影所需的信息。能否请您举个例子?
  • 编辑了更多细节,主要是你需要存储数据直到你的所有处理完成(最好在你的“主”数组中,在相应的项目中)。如果你那么,在 foreach 之后做一个 print_r 你会明白我的意思
  • 太好了,非常感谢您的帮助。我只是想按上述方式实施您的建议,但似乎代码在获取 $rt_id 和 $films['watchlist_films']=$watchlist_films 之间失败了。回显 $rt_id 得到每个观察列表中第一部电影的电影 ID,正如预期的那样,但回显 $films['watchlist_films'] 只会得到一个空数组?
  • 我用不需要的部分来缩短代码,并删除了 Rotten Tomatoe 调用(由/////Rotten Tomato Call ///// 行表示)只是为了节省答案空间。所以你需要调用,就像你的初始代码一样。
猜你喜欢
  • 2021-05-06
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多