# Youseful Software - FileMaker sample script
# Script name: Youseful - PHP totals
# Plug-in: YsflPHP.fmx64 (Windows x64)
# Setup: Install PHP 8 or later for Windows. If discovery fails, add a separate php=<full path to php.exe> option line before param=. No fields needed. JSON is passed through $fm_param.
# Expected: {"count":2,"total":37} (JSON spacing and key order may differ).
# Format: UTF-8 Script Workspace step listing. Add the steps manually; this is not an importable FileMaker file.
# Lines beginning with # are notes; optionally add them as Comment steps.
# Preserve calculation quotes, backslashes, and paragraph symbols exactly.

Set Variable [ $rows ; Value: "[{\"qty\":3,\"price\":10},{\"qty\":1,\"price\":7}]" ]
Set Variable [ $code ; Value:
    "$rows = json_decode($fm_param, true, 512, JSON_THROW_ON_ERROR); " &
    "$total = array_sum(array_map(fn($row) => $row['qty'] * $row['price'], $rows)); " &
    "return ['count' => count($rows), 'total' => $total];"
]
Set Variable [ $json ; Value: YsflPh_Run ( $code ; "mode=process timeout=15¶param=" & $rows ) ]
Set Variable [ $error ; Value: YsflPh_LastError ]
If [ $error <> 0 ]
    Set Variable [ $detail ; Value: YsflPh_Error ]
    Show Custom Dialog [ "PHP failed" ; $detail ]
    Exit Script [ Text Result: $json ]
End If
Show Custom Dialog [ "Summary" ; JSONFormatElements ( $json ) ]
