<?php
session_start();

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "summaryid")
    $id = $paramValue;
  else if ($paramName == "field")
    $field = $paramValue;
}

$dal = new DAL();

if ($field == "cxtfkyInstrumentGroup") {
//  $_SESSION["instrumentgroup"] = $id;
//  $_SESSION["instrumentclass"] = 0;
//  $_SESSION["instrumenttype"] = 0;
    
  $where = "IG.id = $id AND IC.group_id = IG.id " .
           "AND IT.class_id = IC.id AND IM.type_id = IT.id  ";
}

else if ($field == "cxtfkyInstrumentClass") {
 // $_SESSION["instrumentclass"] = $id;
 // $_SESSION["instrumenttype"] = 0;

  $where = "IT.class_id = $id AND IM.type_id = IT.id " .
           "AND IC.id = IT.class_id AND IG.id = IC.group_id ";
}

else if ($field == "cxtfkyInstrumentType") {
  $_SESSION["instrumenttype"] = $id;
    
  $where = "IT.id = $id AND IM.type_id = IT.id " .
           "AND IC.id = IT.class_id AND IG.id = IC.group_id ";
}

$dal = new DAL();
      
$sql = "SELECT IM.id, IM.name AS 'Instrument', " .
       "IG.name AS 'Group', IC.name AS 'Class', IT.name AS 'Type' " .
       "FROM instrument IM, instrumenttype IT, " .
       "instrumentclass IC, instrumentgroup IG " .
       "WHERE $where " ;
$result = $dal->query($sql);


echo json_encode($result);

?>

