<?php
/*
* getAllPhenoexp.php
* http://tasuke.dna.affrc.go.jp/
*
* Author: Norio Tabei
*
* Copyright (c) 2024 National Institute of Agrobiological Sciences
*
*/
  require '../conf/config.php';

  // Block non-Ajax access.
  $request = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) : '';
  if($request !== 'xmlhttprequest') exit;

  //////////////////////////////////  Connecting with DB
  if( !$conn = new mysqli($host, $user, $pswd, $db)) {
    error_log("Connection failed.\n");
    exit;
  }

  ////////////////////////////////// prepare
  $queryExp = "SELECT accession_id, expression FROM sys_phenotype_expression where phenotype_id=?";
  $stmtExp = $conn->stmt_init();
  $stmtExp->prepare($queryExp);

  $res = array();
  $query = "select phenotype_id,phenotype from sys_phenotype";
  $stmtN = $conn->stmt_init();
  $stmtN->prepare($query);
  $stmtN->execute();
  $stmtN->store_result();
  $stmtN->bind_result($phenoid, $phenoname);
  while ($stmtN->fetch()) {
	$stmtExp->bind_param("s", $phenoid);
	$stmtExp->execute();
	$stmtExp->store_result();
	$stmtExp->bind_result($accid, $exp);
	$subArr = array();
	while ($stmtExp->fetch()) {
		$subArr[$accid] = (float) $exp;
	}
	$res[$phenoid] = array($phenoname, $subArr);
  }
  $stmtExp->close();
  $stmtN->close();
  
  echo json_encode($res);
?>

