Thứ Sáu, 20 tháng 6, 2025

Nhập và Lấy ra Json Code ( json_encode + json_decode )

 ## Nhập dữ liệu vào DB  json_encode

$json_data = array();

for($i=0;$i<count($data);$i++){

$title = $data[$i]['g_title'];

$pid = $data[$i]['g_pid'];

$gid = $data[$i]['gid'];

$price = $data[$i]['g_price'];

$cellprice = format_price($price );

$count = $data[$i]['g_count']; 

$point = $data[$i]['g_point'];

$code = $data[$i]['g_code'];

$total = $count * $price;

$celltotal = format_price($total);

$sum = $sum + $total;


$json_data[] = array(

"g_pid"          => $pid,

"g_price" => $price,

"g_point" =>  $point,

"g_count" => $count,

"g_code" => $code,

"g_code" => $client

);

}


$input_data_extend= array(

"dh_text" => json_encode($json_data),

);

Insert vào dữ liệu DB 

$sql= 'INSERT INTO donhang_quanly '.$db->sql_build_array('INSERT', $input_data_extend);


#****# lấy ra json_decode

while($rs = $db->sql_fetchrow($result)){

$obj = json_decode($rs['dh_text']);

for($i=0; $i<count($obj); $i++){

$sum = $sum + ($obj[$i]->g_point*$obj[$i]->g_count);

echo $obj[$i]->g_title;

}

}

Không có nhận xét nào:

Đăng nhận xét