我正在編寫一個腳本,它從一個表(release_dates)獲取信息,但也從另一個表(release_screenshots)獲取X個屏幕截圖的列表。然後它將所有信息放入數組中並將其編碼爲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);
我希望我已經解釋了這不夠好。如果沒有,我可以提供進一步的信息。 任何幫助將不勝感激。
而是兩個查詢,這樣做使兩表的單個查詢。 – Barmar 2014-11-14 20:27:45
...是的,有了一個查詢,你就可以節省時間(加快你的頁面)。但你能否提供「SHOW CREATE TABLE release_screenshot」或澄清「parent_id」在人類文字中的含義? – 2014-11-14 20:31:47
@Barmar - 我對MYSQL和PHP相當陌生。我知道連接表是什麼,但我不知道如何通過連接表來實現我想實現的目標。 – 2014-11-14 20:39:36