我正在编写一个从一个表(release_dates)获取信息的脚本,但也从不同的表中获取X个屏幕截图列表(release_screenshots)。然后它将所有信息放入一个数组中,并将其编码为JSON。
但是,每个JSON条目都有完全相同的屏幕截图列表。
以下是我的代码片段:
$json_response = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['game_title'] = $row['game_title'];
$row_array['game_platform'] = $row['game_platform'];
$row_array['game_genre'] = $row['game_genre'];
$row_array['game_publisher'] = $row['game_publisher'];
$row_array['release_eu'] = $row['release_eu'];
$row_array['release_us'] = $row['release_us'];
$row_array['rrp_gbp'] = $row['rrp_gbp'];
$row_array['rrp_eur'] = $row['rrp_eur'];
$row_array['rrp_usd'] = $row['rrp_usd'];
$row_array['link_address'] = $row['link_address'];
$row_array['logo_image'] = $row['logo_image'];
$row_array['box_art'] = $row['box_art'];
//Build incrementing variable name
$ssno = 1;
$ss = "ss_".$ssno;
//Get the list of screenshots where the referring ID is = to the current ID
$query2 = "SELECT * FROM release_screenshots WHERE parent_id = '$row_array[id]'";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2)){
$array[] = $row2;
}
//Add each Screenshot link to the array build and increment the variable name
foreach($array as $x){
$row_array[$ss] = $x['link'];
$ssno = $ssno + 1;
$ss = "ss_".$ssno;
}
$row_array['youtube_link'] = $row['youtube_link'];
$row_array['notes'] = $row['notes'];
$row_array['entry_created'] = $row['entry_created'];
array_push($json_response,$row_array);
}
echo json_encode($json_response);
我希望我已经解释得这么好了。如果没有,我可以提供进一步的信息。 任何帮助将不胜感激。
答案 0 :(得分:3)
您需要在第二个$array
循环之前清除while
。否则,您将添加上一个游戏中的数组。
$query2 = "SELECT * FROM release_screenshots WHERE parent_id = '$row_array[id]'";
$result2 = mysql_query($query2);
$array = array();
while($row2 = mysql_fetch_array($result2)){
$array[] = $row2;
}
您还需要在外部$row_array
循环的开头清除while
:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array = array();
...