<?php

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

foreach ($_POST as $paramName => $paramValue) {
  if ($paramName == "selectid")
    $selid = $paramValue;
  else if ($paramName == "fields")
    $fields = $paramValue;
  else if ($paramName == "values")
    $values = $paramValue;
}

$dal = new DAL();
$sql = "SELECT  wherefields, " .
       "CASE WHEN combineidx = 1 THEN combinedquery ELSE query END as query " .
       "FROM reportquery " .
       "WHERE id = $selid LIMIT 1 ";
$report = $dal->query($sql);

$fieldList = explode (",", $fields);
$valueList = explode (",", $values);

$sql = str_replace ($fieldList, $valueList, $report->query);

$result = $dal->query($sql);

echo json_encode($result);

?>

