[英]Insert json in mysql database using php in codeigniter
I am getting this response from an android app in JSON, I want to fetch the response and insert each data as a single row below down is my JSON response model and controller thanks in advance我从 JSON 中的 android 应用程序收到此响应,我想获取响应并将每个数据插入为下面的单行是我的 JSON 响应模型和控制器,提前致谢
Response回复
{
"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"
}]
}
controller控制器
$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!");
}
Model模型
public function insertorderinfo($data) {
$this->db->insert('ORDER_INFO', $data);
}
Try This尝试这个
<?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 />";
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.