Updating individual entries in structure via AJAX

Not tested, but I think something like this should work:

if(kirby()->request()->ajax()) {

    $userAnswer = kirby()->request()->query()->name();

    // Get Staff Page
    $staffPage = $pages->findBy('uid', 'staff');
    $staffList = $staffPage->staffers()->yaml();

    foreach($staffList as &$staff) {

        if($staff['name'] = $userAnswer and $staff['checkedin'] == '') {
            $staff['checkedin'] = 'Yes';
        } else if($staff['name'] = $userAnswer and $staff['checkedin'] == 'Yes') {
            $staff['checkedin'] = '';
        };
    }

    $staffPage->update(['staffers' => yaml::encode($staffList)]);
}
else {
    header::status('404');
}

The ‘&’ (reference) in the foreach loop allows you to modify the loop $staff item (it updates the actual element in $staffList). Then you just update the staffers field with the complete updated staff list.

Edit: What context exactly is this executed in? The $this[] does not make much sense to me; what is $this in this particular context?

2 Likes