<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid") 
    $cu_id = $paramValue;  
  else if ($paramName == "rowids") 
    $rowids = $paramValue;  
  else if ($paramName == "fnvals") 
    $functions = $paramValue;  
  else if ($paramName == "selvals") 
    $selects = $paramValue;  
}

$sr_id  =  explode (",", $rowids);
$function = explode (",", $functions);
$select  =  explode (",", $selects);

$dal = new DAL();

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

    $sr_id = hexdec(uniqid());

    $sql = "INSERT INTO siterecommendrationale (id, culturalunit_id, recommendrationale_id) " .
           "VALUES ($sr_id, $cu_id, '$select[$i]') ";
    $result = $dal->query($sql);
  }

  else if ($function[$i] == -1) {
    $sql = "DELETE FROM recommendrationale WHERE id = $sr_id[$i] ";
    $result = $dal->query($sql);
  }
  
  else {
    $sql = "UPDATE siterecommendrationale SET recommendrationale_id='$select[$i]' " .
           "WHERE id = $sr_id[$i] ";
    $result = $dal->query($sql);
  }    
}
    
echo json_encode($cu_id);

?>

