<?php

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

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

$rp_id =  explode (",", $inpids);
$addid = explode (",", $addids);

$dal = new DAL();

for ($i=0; $i<count($rp_id); $i++) {
    
  if ($addid[$i] == 0) {

    $re_id = hexdec(uniqid());

    $sql = "INSERT INTO relatedproject (id, project_id, relatedproject_id) " .
           "VALUES ($re_id, $pr_id, '$rp_id[$i]') ";
    $result = $dal->query($sql);
  }

  else if ($addid[$i] == -1) {
    $sql = "DELETE FROM relatedproject " .
           "WHERE project_id = $pr_id AND relatedproject_id = $rp_id[$i] ";
    $result = $dal->query($sql);
  }
}
    
echo json_encode($pr_id);

?>

