从数据库创建json文件

时间:2021-06-10 23:37:08

标签: php arrays json mysqli

我需要按照以下格式创建这个 json

{
  "status": true,
  "message": "",
  "orders": [
    {
      "orderId": "1",
      "orderDate": "1/06/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "5.00"
        },
        {
          "productName": "Product 2",
          "quantity": "2",
          "price": "24.00"
        },
        {
          "productName": "Product 3",
          "quantity": "1",
          "price": "6.50"
        }
      ]
    },
    {
      "orderId": "2",
      "orderDate": "2/06/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "3.00"
        },
        {
          "productName": "Product 2",
          "quantity": "1",
          "price": "11.50"
        }
      ]
    },
    {
      "orderId": "3",
      "orderDate": "03/05/2021",
      "products": [
        {
          "productName": "Product 1",
          "quantity": "1",
          "price": "3.00"
        },
        {
          "productName": "Product 2",
          "quantity": "1",
          "price": "11.50"
        }
      ]
    }
  ]
}

这是我用来从数据库中检索信息的代码

$stmt = $con->prepare("SELECT OrderID, OrderDate, ProductName, ProductQty, ProductPrice FROM Orders where CustomerID = ?");
$stmt->bind_param("s", $CustomerID);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($OrderID, $OrderDate, $ProductName, $ProductQty, $ProductPrice); 
while($stmt->fetch()) { 
    $message[] = array(
        "status" => true, 
        "message" => "", 
        "orders" => array(
            array( 
                "orderId" => "$OrderID", 
                "orderDate" => "$OrderDate", 
                "products" => array(
                    array( 
                        "productName" => "$ProductName",
                        "quantity" => "$ProductQty", 
                        "price" => "$ProductPrice"
                    ), 
                )
            )
        )
    );
}

$json = $message;

header('content-type: application/json');
echo json_encode($json);

这是来自数据库的信息的显示方式。我不知道如何正确显示有关产品的信息。谁能告诉我如何以我在 php 中要求的格式执行此操作?在此先感谢您的帮助。

[
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "620",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "240",
            "quantity": "1",
            "price": "5.00"
          },
          {
            "productName": "270",
            "quantity": "1",
            "price": "10.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "30",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "280",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  },
  {
    "status": true,
    "message": "",
    "orders": [
      {
        "orderId": "1",
        "orderDate": "1/06/2021",
        "products": [
          {
            "productName": "610",
            "quantity": "1",
            "price": "5.00"
          }
        ]
      }
    ]
  }
]

2 个答案:

答案 0 :(得分:0)

您正在重复您不应该使用的订单结构。这就是为什么你得到错误的结构。这是您应该做的事情(您的代码,已修改):

// specify your return response array only once
$message = array(
    "status" => true,
    "message" => "",
    "orders" => array();
);

$stmt = $con->prepare("SELECT OrderID, OrderDate, ProductName, ProductQty, ProductPrice FROM Orders where CustomerID = ?");
$stmt->bind_param("s", $CustomerID);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($OrderID, $OrderDate, $ProductName, $ProductQty, $ProductPrice); 

while($stmt->fetch()) { 
    // and fill its "orders" member with your orders list
    $message["orders"][] = array(
        "orderId" => "$OrderID", 
        "orderDate" => "$OrderDate", 
        "products" => array(
            array( 
                "productName" => "$ProductName",
                "quantity" => "$ProductQty", 
                "price" => "$ProductPrice"
            ), 
        )
    );
}

$json = $message;

header('content-type: application/json');
echo json_encode($json);

答案 1 :(得分:-1)

你可以像下面这样尝试

const data = [
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "620",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "240",
                        "quantity": "1",
                        "price": "5.00"
                    },
                    {
                        "productName": "270",
                        "quantity": "1",
                        "price": "10.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "30",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "280",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": true,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "610",
                        "quantity": "1",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": false,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "800",
                        "quantity": "50",
                        "price": "5.00"
                    }
                ]
            }
        ]
    },
    {
        "status": false,
        "message": "",
        "orders": [
            {
                "orderId": "1",
                "orderDate": "1/06/2021",
                "products": [
                    {
                        "productName": "1800",
                        "quantity": "50",
                        "price": "5.00"
                    }
                ]
            }
        ]
    }
];

const resutl = data.reduce((acc, cur) => {
    const key = cur.status ? "successItems" : "failureItems";

    if (acc[key]) {
        return {
            ...acc,
            [key]: {
                status: cur.status,
                messgae: '',
                orders: [
                    ...acc[key].orders,
                    ...cur.orders
                ]
            }
        }
    } else {
        return {
            ...acc,
            [key]: {
                status: cur.status,
                messgae: '',
                orders: [
                    ...cur.orders
                ]
            }
        }
    }
}, {});
console.log('resutl', resutl);

console.log('resutl', Object.values(resutl));