[英]foreach only bringing back last item in array
我試圖根據來自兩個不同表的信息從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
}?>
我遇到的問題是,雖然處理部分可以准確地獲得所需的信息(即每個監視列表中第一部電影的名稱和縮略圖),但是當我將這些信息解析到輸出中時, 只有名稱和張貼者顯示了最后一個監視列表中的第一部電影 ,如下所示:
在處理過程中,您覆蓋了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
}?>
全部未測試;-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.