<?php
if (isset($_POST['network'])) {
    include 'dbconnect.php';
    $contactTokens = json_decode($_POST['contactTokens']);
    $sumOfContacts = 0;
  
    // Prepare the SQL query to fetch the matching entries from the database for each contact
    $query = "SELECT number_of_contacts FROM contacts WHERE userid = $1";
    $stmt = pg_prepare($db, "select_query", $query);

    foreach ($contactTokens as $contactTokenInstance) {
        $result = pg_execute($db, "select_query", [$contactTokenInstance]);
        // Fetch the row for the current contact from the database
        $row = pg_fetch_assoc($result);
        if ($row) {
          $sumOfContacts += $row['number_of_contacts'];
        }
    }
   
    if ($result) {
        $res = [
            'success'=>true,
            'network'=>$sumOfContacts,
            
            ];
   
    } else {
         $res = [
            'success'=>false
            
            ];
    }
     echo json_encode($res);

    pg_close($db);
}
?>
