转换为包含3个表

时间:2017-08-13 06:32:14

标签: php mysql json

您对这个主题不熟悉,我真的需要您真正了解此事的帮助。

下图显示了我的数据库设计和查询结果。DB Design

以下代码是我迄今为止尝试过的。

    <?php

include_once './DbConnect.php';

    $db = new DbConnect();

    $response = array();
    $response["content_info"] = array();

    $query = "SELECT Aid, AName, Bid, BName, Cid, CName, Content FROM A, B, C WHERE fk_Aid = Aid AND fk_Bid = Bid";
    $result = mysqli_query($db->getConnection(),$query);


    while($row = mysqli_fetch_array($result)){
        $tmp = array();
        $tmp["Aid"] = $row["Aid"];
        $tmp["AName"] = $row["AName"];
        $tmp["Bid"] = $row["Bid"];
        $tmp["BName"] = $row["BName"];
        $tmp["Cid"] = $row["Cid"];
        $tmp["CName"] = $row["CName"];
        $tmp["Content"] = $row["Content"];

        // push info to final json array
        array_push($response["content_info"], $tmp);

    }

    header('Content-Type: application/json');

    echo json_encode($response);
?>

但它返回以下JSON对象:

{
  "content_info": [
    {
      "Aid": "A1",
      "AName": "AName1",
      "Bid": "B1",
      "BName": "BName1",
      "Cid": "C1",
      "CName": "CName1",
      "Content": "aaaaaaa"
    },
    {
      "Aid": "A1",
      "AName": "AName1",
      "Bid": "B1",
      "BName": "BName1",
      "Cid": "C2",
      "CName": "CName2",
      "Content": "abdsdsfdsf"
    },
    {
      "Aid": "A1",
      "AName": "AName1",
      "Bid": "B2",
      "BName": "BName2",
      "Cid": "C3",
      "CName": "CName3",
      "Content": "dfefeeefeee"
    },
    {
      "Aid": "A1",
      "AName": "AName1",
      "Bid": "B2",
      "BName": "BName2",
      "Cid": "C4",
      "CName": "CName4",
      "Content": "fdsfdfdsf"
    },
    {
      "Aid": "A2",
      "AName": "AName2",
      "Bid": "B3",
      "BName": "BName3",
      "Cid": "C5",
      "CName": "CName5",
      "Content": "fsdfsfsfddf"
    }
  ]
}

但这不是我期望的JSON对象。以下结构是我想要的

{
  "content_info": [
    {
      "Aid": "A1",
      "AName": "AName1",
      "B": [
        {
          "Bid": "B1",
          "BName": "BName1",
          "C": [
            {
              "Cid": "C1",
              "CName": "CName1",
              "Content": "aaaaaaa"
            },
            {
              "Cid": "C2",
              "CName": "CName2",
              "Content": "abdsdsfdsf"
            }
          ]
        },
        {
          "Bid": "B2",
          "BName": "BName2",
          "C": [
            {
              "Cid": "C3",
              "CName": "CName3",
              "Content": "dfefeeefeee"
            },
            {
              "Cid": "C4",
              "CName": "CName4",
              "Content": "fdsfdfdsf"
            }
          ]
        }
      ]
    },
    {
      "Aid": "A2",
      "AName": "AName2",
      "B": [
        {
          "Bid": "B3",
          "BName": "BName3",
          "C": [
            {
              "Cid": "C5",
              "CName": "CName5",
              "Content": "fsdfsfsfddf"
            }
          ]
        }
      ]

如果有人知道你能告诉我我需要编辑什么。 谢谢任何尝试这一点的人。

2 个答案:

答案 0 :(得分:0)

save(form: any) {
    var reportPersonList: ReportPerson[] = new Array();

    var people = form.get('people') as FormArray;

    for (let i = 0; i < people.length; i++) {
        var p = new ReportPerson;
        p.lastName = people.at(i).get('lastName').value;
        p.firstName = people.at(i).get('firstName').value;
        p.middleName = people.at(i).get('middleName').value;
        var addresses = people.at(i).get('addresses') as FormArray;
        for (let j = 0; j < addresses.length; j++) {
            var a = new PersonAddress;
            a.street = addresses.at(j).get('street').value;

            p.addresses.push(a);
        };
        reportPersonList.push(p);
    }
    this.reportFormDataService.setReportPeople(reportPersonList);

}

答案 1 :(得分:0)

将此设置为回显关联数组的json_encode($ response,true)