I have the below php code that receives some variables from another page (sent via POST).
Based on the Ad_id and Name... i'm trying to toggle the status between 'active or 'inactive'.
However when i execute this (with correct POST data)... it doesn't seem to update anything. What am i doing wrong?
e.g. When i send through a valid ad, name & status... then go and check the JSON file, it asn't toggled 'active' to 'inactive'.
PHP:
<?php
// Get the post variables
$ad_id = $_GET['ad'];
$name = $_GET['name'];
$status = $_GET['status'];
// Get the JSON file
$json = file_get_contents('test.json');
// Decode Json into an array
$json = json_decode($json,true);
// Within the array.. find the ad that matches the POST variable.
foreach ($json['ads'] as $ad) {
if ($ad['id'] == $ad_id) {
// Within that Ad... find the candidate that matches the POST variable.
foreach ($ad['candidates'] as $candidate) {
if ($candidate['name'] == $name) {
// Within that candidate... check the value of 'status'.
if ($candidate['status'] == 'active') {
// If active, update the status to 'inactive'.
$candidate['status'] = 'inactive';
} else {
// If inactive, update the status to 'active'.
$candidate['status'] = 'active';
}
}
}
}
}
// Encode the array as JSON again
$json = json_encode($json);
// Save the JSON back to the server
file_put_contents('test.json', $json, LOCK_EX);
?>
JSON:
{
"ads": [
{
"id": "12345678",
"hirername": "Demo Bar",
"candidates": [
{
"status": "active",
"name": "Gregory Jones",
"dist": "Richmond (4km away)",
"exp1": "Barman at City Bar for 2 years",
"avail1": "Mon to Fri - Morning, Evening & Night",
"visa": "Australian Citizen",
"call": "0413451222"
},
{
"status": "active",
"name": "Jackie Linton",
"dist": "Box Hill (13km away)",
"exp1": "Bar girl at Collins Bar for 1 year",
"avail1": "Mon to Fri - Morning & Evening",
"visa": "Working holiday visa",
"call": "0413456555"
}
]
}
]
}