0

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 protected]","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 protected]","StoreNumber":"300"}]
7
  • 1
    What do you want the combined result to look like?
    – Barmar
    Mar 7, 2013 at 2:52
  • First of all, you need to think about why you've done this: $row = mysql_fetch_assoc(..) then foreach $row to create exactly the same array under a different name $arr
    – Popnoodles
    Mar 7, 2013 at 2:52
  • Why are you creating an array in Code 1? There can only be one marketplace with ID = 1.
    – Barmar
    Mar 7, 2013 at 2:53
  • @Barmar they are two different queries.. I want my results to contain all the locations in one array, rather than two separate arrays that are currently split by two different logics, that I also want to comibine. Mar 7, 2013 at 2:57
  • Can you post in the question how you want the results to look.
    – Popnoodles
    Mar 7, 2013 at 2:58

2 Answers 2

0

First, drop this

  foreach($row as $key => $value){
      $arr[$key] = $value;
  }

It does nothing except the same as $arr = $row; Actually it does something else - produces a warning as $arr is undefined.

EDIT Updated answer now that the initial query has been changed from WHERE MarketplaceID=1 to WHERE Status = 1"

$main_arr=array();
$sql = "SELECT * FROM Marketplaces WHERE Status = 1";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){   
  $row['locations']=array();
  $sql2 = "SELECT * FROM Locations WHERE MarketplaceID = ".$row['MarketplaceID'];
  $results2 = mysql_query($sql2);
  while($row2 = mysql_fetch_assoc($results2)){
      $row['locations'][]=$row2;
  }
  $main_arr[] = $row; 
}

// print_r($main_arr); and view page source to read, rather than a JSON string

Actually I would do this slightly differently so that my array doesn't muddle locations inside of Marketplace but still keeps them together, like this below

while($row = mysql_fetch_assoc($result)){   
  $thisrow=array('marketplace'=>$row, 'locations'=>array());
  $sql2 = "SELECT * FROM Locations WHERE MarketplaceID = ".$row['MarketplaceID'];
  $results2 = mysql_query($sql2);
  while($row2 = mysql_fetch_assoc($results2)){
      $thisrow['locations'][]=$row2;
  }
  $main_arr[] = $thisrow; 
}

so the object would be

{ 
    "marketplace": {"MarketplaceID": 1, "BusinessName": "Fred Bloggs", ... }
    "locations": [
        {"LocationId": 1, "Address1": "Some address"},
        {"LocationId": 2, "Address1": "Some address"}
    ]
}
5
  • can I pass the MarketplaceID into the second query. I tried this and did not work: $sql = "SELECT * FROM Locations WHERE MarketplaceID = '".$marketplace['MarketplaceID']."'"; Mar 7, 2013 at 3:15
  • I apologize for the confusion and trying to simplify in order to make it easier, but this has just made it tuffer =P. I now understand a bit more how you saw everything and you saw the big picture without me explaining. The first query is updated to this (more than 1 location): $sql = "SELECT * FROM Marketplaces WHERE Status = 1"; and the second $sql = "SELECT * FROM Locations WHERE MarketplaceID = '".$marketplace['MarketplaceID']."'"; How can I add the for each back to make this work? Mar 7, 2013 at 3:22
  • ok. I've just added an addendum as to how I would actually do this, having thought about it.
    – Popnoodles
    Mar 7, 2013 at 3:42
  • Keeping the same logic you are familiar with. If I were to add Programs as a child to Locations and Rewards as a child to Programs. How would I handle that? Here is my code that is NOT nesting in the HTML section: jsfiddle.net/rHgS3 @phpnoodles Mar 7, 2013 at 7:01
  • What you've written ins close but you need to be creating arrays within the arrays not adding to $row, e.g. $row['locations']['programs']=array(); $row['locations']['programs'][]=$row3;, $row['locations']['programs']['rewards']=array(); $row['locations']['programs']['rewards'][]=$row4;
    – Popnoodles
    Mar 7, 2013 at 11:30
0

If I'm reading it correctly, your first array is a single dimension array that houses the Marketplace 1 data, and your second array is a multidimensional array where each element in the parent array contains a child array which contains information about a single location. If you want to place the second array within the first, try nesting your loops like this:

$sql = "SELECT * FROM Marketplaces WHERE MarketplaceID = 1";
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)){   

  foreach($row as $key => $value){
  $arr[$key] = $value;

}

$sql2 = "SELECT * FROM Locations WHERE MarketplaceID = 1";
$results2 = mysql_query($sql2);

while($row2 = mysql_fetch_assoc($results2)){

  foreach($row2 as $key => $value){
  $arr2[$key]=$value;

}

$arr['locations'] = $arr2;

$main_arr = $arr;

}

This creates a temporary array, $arr2, that houses your location data, and then stores it in $arr['locations']. At the end of the code, the $arr that you're adding to $main_arr should have the following elements: "MarketplaceID","AccountID","BusinessName","BusinessType","Status","CreationDate","location", and "location" should be an array containing elements as arrays containing the location information pulled from your database.

Hope this answers your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.