Reputation: 184
I am writing a script that takes information from one table (release_dates), but also takes a list of X number of screen shots from a different table (release_screenshots). It then puts all the info into an array and encodes it as JSON.
However, each JSON entry has exactly the same list of screen shots.
Here is a snippet of my code:
$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);
I hope I have explained this well enough. If not, I can give further information. Any help will be greatly appreciated.
Upvotes: 0
Views: 55
Reputation: 781833
You need to clear $array
before the second while
loop. Otherwise, you're adding to the array from the previous game.
$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;
}
You also need to clear $row_array
at the beginning of the outer while
loop:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$row_array = array();
...
Upvotes: 3