在 codeigniter 中使用 php 在 mysql 数据库中插入 json
Insert json in mysql database using php in codeigniter
我从 JSON 中的 android 应用程序收到此响应,我想获取响应并将每个数据作为一行插入下面是我的 JSON 响应模型和控制器提前致谢
回应
{
"DEALS": [{
"ORDER_DATE": "2020-09-06 03:31:23 PM",
"CUSTOMER_ID": "umersaleem_03334033313",
"QUANTITY": 1,
"DEAL_ID": "3",
"ORDER_TOTAL": "2100.0"
}, {
"ORDER_DATE": "2020-09-06 03:31:23 PM",
"CUSTOMER_ID": "umersaleem_03334033313",
"QUANTITY": 1,
"DEAL_ID": "3",
"ORDER_TOTAL": "2100.0"
}]
}
控制器
$reponse=array();
$this->db->trans_begin();
foreach($data as $row) {
$filter_data = array(
'ORDER_DATE'=> $order_date,
'CUSTOMER_ID' => $customer_id,
'PRODUCT_ID' => $product_id,
'QUANTITY' => $quantity,
'DEAL_ID' => $deal_id,
'ORDER_TOTAL' => $order_total,
);
//Call the save method
$this->Order_model->insertorderinfo($filter_data);
}
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
echo json_encode("Failed to Save Data");
} else {
$this->db->trans_commit();
echo json_encode("Success!");
}
型号
public function insertorderinfo($data) {
$this->db->insert('ORDER_INFO', $data);
}
试试这个
<?php
$test_json='{"DEALS":[{"ORDER_DATE":"2020-09-06 03:31:23 PM","CUSTOMER_ID":"umersaleem_03334033313","QUANTITY":1,"DEAL_ID":"3","ORDER_TOTAL":"2100.0"},{"ORDER_DATE":"2020-09-06 03:31:23 PM","CUSTOMER_ID":"umersaleem_03334033313","QUANTITY":1,"DEAL_ID":"3","ORDER_TOTAL":"2100.0"}]}';
$json=(json_decode($test_json, true));
foreach ($json as $key => $value) {
// get count as in key there is deals
$total_order=count($value);
for($i=0; $i<$total_order; $i++){
echo $value[$i]['ORDER_DATE']."<br />";
echo $value[$i]['CUSTOMER_ID']."<br />";
echo $value[$i]['QUANTITY']."<br />";
echo $value[$i]['DEAL_ID']."<br />";
echo $value[$i]['ORDER_TOTAL']."<br />";
}
}
?>
我从 JSON 中的 android 应用程序收到此响应,我想获取响应并将每个数据作为一行插入下面是我的 JSON 响应模型和控制器提前致谢
回应
{
"DEALS": [{
"ORDER_DATE": "2020-09-06 03:31:23 PM",
"CUSTOMER_ID": "umersaleem_03334033313",
"QUANTITY": 1,
"DEAL_ID": "3",
"ORDER_TOTAL": "2100.0"
}, {
"ORDER_DATE": "2020-09-06 03:31:23 PM",
"CUSTOMER_ID": "umersaleem_03334033313",
"QUANTITY": 1,
"DEAL_ID": "3",
"ORDER_TOTAL": "2100.0"
}]
}
控制器
$reponse=array();
$this->db->trans_begin();
foreach($data as $row) {
$filter_data = array(
'ORDER_DATE'=> $order_date,
'CUSTOMER_ID' => $customer_id,
'PRODUCT_ID' => $product_id,
'QUANTITY' => $quantity,
'DEAL_ID' => $deal_id,
'ORDER_TOTAL' => $order_total,
);
//Call the save method
$this->Order_model->insertorderinfo($filter_data);
}
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
echo json_encode("Failed to Save Data");
} else {
$this->db->trans_commit();
echo json_encode("Success!");
}
型号
public function insertorderinfo($data) {
$this->db->insert('ORDER_INFO', $data);
}
试试这个
<?php
$test_json='{"DEALS":[{"ORDER_DATE":"2020-09-06 03:31:23 PM","CUSTOMER_ID":"umersaleem_03334033313","QUANTITY":1,"DEAL_ID":"3","ORDER_TOTAL":"2100.0"},{"ORDER_DATE":"2020-09-06 03:31:23 PM","CUSTOMER_ID":"umersaleem_03334033313","QUANTITY":1,"DEAL_ID":"3","ORDER_TOTAL":"2100.0"}]}';
$json=(json_decode($test_json, true));
foreach ($json as $key => $value) {
// get count as in key there is deals
$total_order=count($value);
for($i=0; $i<$total_order; $i++){
echo $value[$i]['ORDER_DATE']."<br />";
echo $value[$i]['CUSTOMER_ID']."<br />";
echo $value[$i]['QUANTITY']."<br />";
echo $value[$i]['DEAL_ID']."<br />";
echo $value[$i]['ORDER_TOTAL']."<br />";
}
}
?>