Wednesday 15 May 2013

php - Looped array not giving the desired result -



php - Looped array not giving the desired result -

i writing script takes info 1 table (release_dates), takes list of x number of screen shots different table (release_screenshots). puts info array , encodes json.

however, each json entry has same list of screen shots.

here snippet of 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 list of screenshots referring id = current id $query2 = "select * release_screenshots parent_id = '$row_array[id]'"; $result2 = mysql_query($query2); while($row2 = mysql_fetch_array($result2)){ $array[] = $row2; } //add each screenshot link array build , increment variable name foreach($array $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 have explained enough. if not, can give farther information. help appreciated.

you need clear $array before sec while loop. otherwise, you're adding array previous game.

$query2 = "select * release_screenshots parent_id = '$row_array[id]'"; $result2 = mysql_query($query2); $array = array(); while($row2 = mysql_fetch_array($result2)){ $array[] = $row2; }

you need clear $row_array @ origin of outer while loop:

while ($row = mysql_fetch_array($result, mysql_assoc)) { $row_array = array(); ...

php mysql arrays json foreach

No comments:

Post a Comment