<?php

require_once($_SERVER['DOCUMENT_ROOT'] . '/sys/db.php');

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selid") 
    $pr_id = $paramValue;  
  else if ($paramName == "ids") 
    $ids = $paramValue;  
  else if ($paramName == "chkids") 
    $chkids = $paramValue;  
}

$fw_id =  explode (",", $ids);
$chkid = explode (",", $chkids);

$dal = new DAL();

for ($i=0; $i<count($fw_id); $i++) {
    
  if ($chkid[$i] == 0) {
    $sql = "DELETE FROM projectframework " .
           "WHERE project_id = $pr_id AND framework_id = $fw_id[$i] ";
    $result = $dal->query($sql)->id;
  }
  else {
    $sql = "SELECT id FROM projectframework " .
           "WHERE project_id = $chkid[$i] AND framework_id = $fw_id[$i] ";
    $result = $dal->query($sql);
    
    if (count ($result) == 0) {

      $sql = "INSERT INTO projectframework (project_id, framework_id) " .
             "VALUES ($chkid[$i], $fw_id[$i]) " ;
      $result = $dal->query($sql);
    }
  }
}
    
echo json_encode($pr_id);

?>

