我想从基于来自两个不同表的信息提取数据,然后循环遍历foreach并将这些数据解析为查询。我已经构建了一系列查询,可以获取我正在查找的确切数据(数组中第一部影片的名称和海报),但是当将这些数据解析为返回一系列影片列表的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_item”。 – ethrbunny 2013-03-25 19:31:33
你确定'$ profile_info ['id']'只能包含数字吗?另请注意,不建议使用'mysql_ *'函数(请参阅[红盒子](http://php.net/mysql_query))。 – 2013-03-25 19:37:09
@MarcelKorpel是的,配置文件只能通过其配置文件ID标识为唯一标识符,该标识符始终是一个数字,每个监视列表和每个用户都有一个唯一的ID,并且两者可以相互关联,即每个独特的用户可以有许多监视列表,每个监视列表都有自己的唯一ID。 – 2013-03-25 19:39:07