Recursive PHP

Ask your questions here.
Post Reply
ucupable
Posts: 3
Joined: 06 Apr 2023, 11:17
Name: Ucup ucup
Location: Indonesia, Jakarta

Recursive PHP

Post by ucupable »

Code: Select all

// Fungsi untuk menghitung jumlah total data berdasarkan parent_id
function hitungJumlahData($parent_id) {
    $query = "SELECT IFNULL(sum(field_***), [***]) AS total_data FROM app_entity_*** WHERE parent_id = $parent_id";
    $result = db_query($query);
    $row = mysqli_fetch_assoc($result);
    $totalData = $row['total_data'];

    // Panggil rekursif untuk setiap child yang ada
    $queryChild = "SELECT id FROM app_entity_*** WHERE parent_id = $parent_id";
    $resultChild = db_query($queryChild);
    while ($rowChild = mysqli_fetch_assoc($resultChild)) {
        $childId = $rowChild['id'];
        $totalData += hitungJumlahData($childId);
    }

    return $totalData;
}

// Memanggil fungsi hitungJumlahData dengan parent_id awal
$parent_id_awal = [id]; // Ganti dengan parent_id awal yang sesuai
$total_data = hitungJumlahData($parent_id_awal);

// Menampilkan hasil
$current_field_value = "Rp ".str_replace(',', '.',number_format($total_data,0,'.'));
    $output_value = $current_field_value;  
the code is calculate value base on descent parent_id using recursive sistem, work normal but when in listing get error 500.
or anyone have better idea?
Post Reply