recently im trying to work with JSON data via php for my higher education project. But I hanged-up while managing/updating this into the JSON file.
Let me introduce you the whole scenario:
Its a entry based project, where all entries must be save in the JSON file (.json), format is :
[{"Name":"xyz","UID":"1234","RDate":"11122014","ReLogin":"true","LTime":"1e3","Passive":"false"},{"Name":"abc","UID":"5678","RDate":"01102014","ReLogin":"true","LTime":"1e3","Passive":"false"},......]
Now, what I have to do is that: when a new entry sended to the PHP, than firstly find the match entry based on some paremater, like name, uid, rdate.
IF MATCH FOUND, than replace/update the whole entry into the file,
IF MATCH NOT FOUND, than add/write the new entry into the file.
To do this, currently I'm trying to use these bunch of codes:
1) `function chkEntry($uid,$entry)
$file='file.json';
$s[]=$entry;$match=0;
global $RESP;
if(0==filesize($file)){
return true;
}else{
$data=json_decode(file_get_contents($file),true);
foreach($data as $keyD=>$valD){
foreach($s as $keyS=>$valS){
if($valD["Name"]!=$valS["Name"]||$valD["UID"]!=$valS["UID"]||$valD["RDate"]!=$valS["RDate"]){ // here `RDate` is always unique, so I can use it for the matching process
$match=0;
}else{
$match=1;
}
}
}
if($match==1){return false;}else{return true;}
}
This chkEntry always return false :(
And also I don't know that how to replace the while entry int the file, if match found.
UPDATED chkEntry function: matching criteria is limited to a single parameter only [uid]. If match found, than delete the whole entry and return true to the addEntry function, so that addEntry add the new entry.But unfortunately this function is also not working as expected.
function chkEntry($uid,$entry){
$file='file.json';
$data=json_decode(file_get_contents($file),true);
foreach($data as $keyD=>$valD){
if($valD["uid"]==$entry["uid"]){
unset($data[$keyD]); //delete the entry, if matched
file_put_contents($file,json_encode($data));
return true;
}
}
return true;
}
2) `function addEntry($uid,$entry)
global $RESP;
$ready=false;
$file='file.json';
if(addFile($uid)){ //`function addFile`, if file not exists, than create an blank file
$data=json_decode('file.json',true);
unset($file);
if(chkEntry($uid,$entry)){ //`function chkEntry`, match the entry
$data[]=$entry;
file_put_contents('file.json',json_encode($data));
unset($data);
}else{
$RESP["entry"]="This entry is already exists into Database";
}
$ready=true;
}else{
$RESP["err"]="We are unable to add the entry right now. Please try after some time!";
}
if($ready){
if(!isset($RESP["entry"])){$RESP["entry"]="for evaluatuion";}
return true;
}else{
$RESP["err"]="Something went wrong! Please try to add this later.";
return false;
}
3)
$entry=array("Name"=>$_POST["name"],"UID"=>$_POST["uid"],"RDate"=>$_POST["rdate"],"ReLogin"=>$_POST["relogin"],"LTime"=>$_POST["ltime"],"Passive"=>$_POST["passive"]);
addServer($uid,$entry);
So, my question is :
How the finding/matching function should be managed, so that its return true if match found otherwise return false and IF MATCH FOUND, than how to replace/update the whole entry into the file.
for example: if the new entry is
{"Name":"try","UID":"1111","RDate":"12122014","ReLogin":"`true","LTime":"1e3","Passive":"false"}
than first match it to the existing entries and than ad it to the file.
IF the new entry is : (based on name, uid, rdate)
{"Name":"xyz","UID":"2222","RDate":"15122014","ReLogin":"true","LTime":"1e3","Passive":"false"} //match found, based on `name`
OR
{"Name":"nws","UID":"1234","RDate":"19122014","ReLogin":"true","LTime":"1e3","Passive":"false"} //match found, based on `uid`
OR
{"Name":"nws","UID":"2222","RDate":"11122014","ReLogin":"true","LTime":"1e3","Passive":"false"} //match found, based on `rdate`
than update/replace the old entry {"Name":"xyz","UID":"1234","RDate":"11122014","ReLogin":"true","LTime":"1e3","Passive":"false"} with it.
help me plz.....
Thanks & Regards