0

I have two table names are channel and department.

$result = mysql_query("SELECT * FROM channels");
while($json = mysql_fetch_assoc($result)){
    $fetch = mysql_query("SELECT * FROM departments where channelid='".$json['id']."'"); 
    $json2 = array();
    while ($row = mysql_fetch_array($fetch)){
        $json2[] = array('departmentname' => $row["departmentname"],'departmentid' => $row["did"]    
        );
    }
    $json['department'] = $json2;
    echo json_encode($json);
}

Ouput:

{
  "id": "1",
  "channelname": "con",
  "channelurl": "http:\/\/xxx.net\/YY\/",
  "department": [
    {
      "departmentname": "xxx Travel",
      "departmentid": "1"
    },
    {
      "departmentname": "xxxx Virtual Assist",
      "departmentid": "2"
    },
    "departmentname": "xxx  Premier",
    "departmentid": "3"
  },
  {
    "departmentname": "xxxx Events",
    "departmentid": "4"
  }
]
}{
"id": "2",
"channelname": "Slim",
"channelurl": "http:\/\/xxxxx.net\/slim\/",
"department": [
  {
    "departmentname": "Virtualvideo",
    "departmentid": "5"
  }
]
}

Expected Result:

[
  {
    "id": "1",
    "channelname": "xxxxx",
    "channelurl": "http://XXXXX.net/yyy/",
    "department": [
      {
        "departmentname": "XXX Travel",
        "departmentid": "1"
      },
      {
        "departmentname": "XXXX Virtual Assist",
        "departmentid": "2"
      },
      {
        "departmentname": "XXXX Premier",
        "departmentid": "3"
      },
      {
        "departmentname": "XXXX Events",
        "departmentid": "4"
      }
    ]
  },
  {
    "id": "2",
    "channelname": "Slim",
    "channelurl": "http://XXXXXX.net/slim/",
    "department": [
      {
        "departmentname": "Virtual video",
        "departmentid": "5"
      }
    ]
  }
]
5
  • 2
    Would you clarify differences of output result with expected one? Commented Jan 8, 2014 at 12:01
  • Possible duplicate: stackoverflow.com/questions/13205877/… Commented Jan 8, 2014 at 12:03
  • look at the second array of parent object. Commented Jan 8, 2014 at 12:04
  • Please spell it out for us, nobody enjoys deciphering super-long right scrolling lines of minified code/data. Commented Jan 8, 2014 at 12:07
  • echo after while loop close Commented Jan 8, 2014 at 12:11

2 Answers 2

1

try this:

$json_channel = array();
$json_final = array();
$result = mysql_query("SELECT * FROM channels");
while($json = mysql_fetch_assoc($result))
{
    $fetch = mysql_query("SELECT * FROM departments where channelid='".$json['id']."'");     
    $json_dept = array();

    while ($row = mysql_fetch_array($fetch))
    {
        $json_dept[] = array('departmentname' => $row["departmentname"],'departmentid' => $row["did"]);
    }

    $json_channel = array('id' => $json["ch_id"], 'channelname' =>$json["channelname"], 'department' => $json_dept);    

    $json_final[] = $json_channel;
}
echo json_encode($json_final);
Sign up to request clarification or add additional context in comments.

Comments

0
$output = array();
$result = mysql_query("SELECT * FROM channels");
while($json = mysql_fetch_assoc($result)){

    $fetch = mysql_query("SELECT * FROM departments where channelid='".$json['id']."'"); 
    $json2 = array();
    while ($row = mysql_fetch_array($fetch)){
        $json2[] = array('departmentname' => $row["departmentname"],'departmentid' => $row["did"]    
        );
    }

    $json['department'] = $json2;
    $output[] = $json;   
}
echo json_encode($output);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.