dimanche 19 avril 2015

PHP and JSON output with and without key

I have a weird bug, and uncertain what causes it. Perhaps there is an error somewhere in my code.


This is the JSON that PHP (via Ajax) sends to the browser for JQuery to use, and this is the correct way, and how i want it.



{"response":"success","comment":"<strong>Done.<\/strong> Your details has been updated.","id":"4","images":[{"Image":"\/cache\/Products\/picture.jpg"},{"Image":"\/cache\/Products\/picture2.jpg"}]}


But the odd occasion i get the following:



{"response":"success","comment":"<strong>Done.<\/strong> Your details has been updated.","id":"4","images":{"1": {"Image":"\/cache\/Products\/picture.jpg"},"2":{"Image":"\/cache\/Products\/picture2.jpg"}}}


The Images JSON array changes! Yet they both use the same code.


This is the PHP case, that Ajax calls.



case 'removeImages':
$id = fixText($user, $_GET['id'],0,0,0);
$elm = fixText($user, $_POST['elm'],0,0,0);
$uploadTo = fullPath;
$result = mysqli_query($user, "SELECT productImages FROM hireProducts WHERE `id`='$id'");
if ($row = mysqli_fetch_assoc($result)) {
$get = (array) json_decode($row['productImages'], true);
$filename = $uploadTo.$get[$elm]['Image'];
if (file_exists($filename)) {
unlink($filename);
}
unset($get[$elm]);

$jsonImages = json_encode($get);
$update = mysqli_query($user, "UPDATE hireProducts SET `productImages` = '$jsonImages' WHERE `id`='$id'");
if ($update) {
$arr = array('response'=>'success','comment'=>A01, 'id'=>$id, 'images'=>$get);
} else {
$arr = array('response'=>'error','comment'=>A001.'AL001');
}
} else {
$arr = array('response'=>'error','comment'=>A003);
}
echo json_encode($arr);
exit;
break;


All i want is for it to always to be the same (the first one). Thanks


Aucun commentaire:

Enregistrer un commentaire