Merhaba, aşağıda yaptığım bir küçük hata nedeniyle yapmak istediğimi yapamıyorum...
$user_id = 101;
$file = "user-".$user_id;
$db_name = $file;
$new = array('sure'=>5, 'catid'=>2,'type'=>"t2", 's'=>"s2", 'amount'=>6);
echo UpdateCart($db_name,$new);
function UpdateCart($db_name,$data){
$values = jsondb_get($db_name);
//$values
/*
{"data":
[
{"sure":2,"catid":1,"type":"t1","s":"s1","amount":1},
{"sure":2,"catid":2,"type":"t2","s":"s2","amount":1},
{"sure":2,"catid":3,"type":"t3","s":"s3","amount":1}
]
}
*/
$db_names = $db_name . ".json";
$filename = $db_names;
//data
$n_id = $data['catid'];
$n_type = $data['type'];
$n_s = $data['s'];
# check file
if(is_file($filename)){
foreach($values['data'] as $value){
//id
$o_id = $value['catid'];
//type
$o_type = $value['type'];
//s
$o_s = $value['s'];
//amount
$o_split = $value['amount'];
$n_split = $data['amount'];
$updateAmount = $value['amount']+$data['amount'];
if($o_id == $n_id && $o_type == $n_type && $o_split == $n_split){
$values['amount'] = $updateAmount;
$values['data']['amount'] = $updateAmount;
}
}
file_put_contents($db_names, json_encode($values));
$filefound = "data is update-".$updateAmount;
} else {
$filefound = add_new($data,$db_name);
}
return $filefound;
}oluşan/varolan dosya dosya...
{"data":
[
{"sure":2,"catid":1,"type":"t1","s":"s1","amount":1},
{"sure":2,"catid":2,"type":"t2","s":"s2","amount":1},
{"sure":2,"catid":3,"type":"t3","s":"s3","amount":1}
]
}Olması gereken dosya.
{"data":
[
{"sure":2,"catid":1,"type":"t1","s":"s1","amount":1},
{"sure":2,"catid":2,"type":"t2","s":"s2","amount":7},
{"sure":2,"catid":3,"type":"t3","s":"s3","amount":1}
]
}