0

i have some json files which look like this:

{
    "id": "id_11-08-2021",
    "name": "John",
    "date": "11-08-2021",
    "day": "Wednesday",
    "starttime": "08:30",
    "endtime": "10:00",
    "hours": 1.5
}

To read all the json files in a dir i use this code (process.php):

$files = glob('data/*.json'); // all json files in dir data
foreach($files as $file) {
    $objs[] = json_decode(file_get_contents($file),1); // all json objects in array             
}
$result = [];
foreach($objs as $key => $val) {
    $result['data'] = array(                                
                        'id' => $val['id'];
                        'date' => $val['date'];
                        'day' => $val['day'];
                        'starttime' => $val['starttime'];
                        'endime' => $val['endtime'];
                        'hours' => $val['hours']                                    
                        );
}
$result['name'] = $_POST['name'];
$result['status'] = 2;
echo json_encode($result); // send back data

My jquery ajax looks like this:

$.ajax({
        type: "POST",
        url: "process.php",
        data: {
            name: name          
        },
        dataType: "json",
        success: function(response) {
            if(response.status == 2) { // check status of data
                $('tbody').html(response.data); // handle data from server; output data in tbody
                $('.name').html(response.name); // name from user
            }
        },
});

I do not receive the data from the foreach loop. What is going wrong?

1
  • First of all, you are overwriting $result['data'] in each loop iteration, that should be $result['data'] = ... Commented Aug 25, 2021 at 8:41

1 Answer 1

2

First of all, you're overwriting your result in the loop constantly. You need to create a new array element within data in the loop.

$result = [];
foreach ($objs as $key => $val) {
    $result['data'][] = array(                                
        'id' => $val['id'];
        'date' => $val['date'];
        'day' => $val['day'];
        'starttime' => $val['starttime'];
        'endime' => $val['endtime'];
        'hours' => $val['hours']                                    
    );
}
$result['name'] = $_POST['name'];
$result['status'] = 2;

Secondly, since you're not changing anything in your loop, you could just use your $objs array and remove the loop. The loop doesn't do anything in your example.

$result['data'] = $objs;
Sign up to request clarification or add additional context in comments.

7 Comments

Did you mean $result['data'][] = $objs; for the last example?
@ADyson No, because objs is already an array. $result['data'] should be the same after the loop as if you are doing $result['data'] = $objs. $result['data'][] = $objs would result in an array like data -> result -> 0 -> array of jsons
If you're saying it should be $result['data'][] = array( in the first example, then it should be $result['data'][]in the second example. Otherwise it's not equivalent, and you'll end up overwriting $result['data'] again every time the foreach loop runs - which was the original problem.
@ADyson No, because the first one is a loop that fills the $result['data'] array with entries. $result['data'][] creates a new array entry within the data key. But since $objs already is an array there is not necessity to create a new array entry within result. Because the line $result['data'] = $objs; would replace the loop. You are correct if you'd do the loop and the line $result['data'] = $objs;, but then you would duplicate the data. The line $result['data'] = $objs; would replace the loop.
@john A 500 error is a server-side error, so it cannot becaused by jQuery directly. Maybe you mean the request which resulted in a 500 was triggered by a jQuery AJAX request, or something? Either way, you need to look in the server's PHP error log to find the underlying error - a 500 code on its own doesn't tell us much.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.