I have two database tables. One contains Marketplaces and the other contains the Locations of the Marketplaces. Marketplaces can have multiple locations the two tables are joined by the MarketplaceID. I need to query the marketplaces turn it into an array and during the foreach or the while, add the locations as an array to the marketplaces. This is all part of two json responses. However my goal is to combine the data together and have one json response where the locations are within the marketplaces json data as "Locations". Ultimately an array where Marketplaces is the parent and Locations are the children.
Code 1 (Table1 Query):
$sql = "SELECT * FROM Marketplaces WHERE Status = 1";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
foreach($row as $key => $value){
$arr[$key] = $value;
}
$main_arr[] = $arr;
}
return $main_arr;
Result:
[{"MarketplaceID":"1","AccountID":"0","BusinessName":"Mike's Pub","BusinessType":"1","Status":"1","CreationDate":"0000-00-00 00:00:00"}]
Code 2 (Table2 Query):
$sql = "SELECT * FROM Locations WHERE MarketplaceID = 1";
$results = mysql_query($sql);
while($row = mysql_fetch_assoc($results)){
foreach($row as $key => $value){
$arr[$key]=$value;
}
$main_arr[] = $arr;
}
Result:
[{"LocationID":"1","MarketplaceID":"1","Address1":"5055 Business Center Drive","Address2":"Suite 100","City":"San Francisco","CA":"1","Zip":"90210","Phone1":"(555) 555-555","Phone2":"(555) 555-5555","EmailAddress":"email@msn.com","StoreNumber":"200"},{"LocationID":"2","MarketplaceID":"1","Address1":"112 Street Court","Address2":"","City":"Los Angeles","StateID":"2","Zip":"90210","Phone1":"(555) 555-6666","Phone2":"","EmailAddress":"email@gmail.com","StoreNumber":"300"}]