<?php

  require '../conf/config.php';

  //////////////////////////////////  Connecting with DB
  $conn = mysql_connect($host, $user, $pswd);
  $db = mysql_select_db($db);

  ////////////////////////////////////////////////////////// List of accessions
  $ids = array();
  $dbs = array();
  $grp = array();
  $query = "SELECT accession_id, accession_db, accession_name, variety, sub_variety, origin, origin_2, type FROM sys_accession";
  $result = mysql_query($query);
  while ($row = mysql_fetch_row($result)) {
    $ids[]=$row[0];
    $dbs[]=$row[1];
    $names[]=$row[2];
    $variety[]=$row[3];
    $sub_variety[]=$row[4];
    $origins[]=$row[5];
    $origins_2[]=$row[6];
    $type[]=$row[7];
  }

  $str="[";
  for ($i=0; $i<count($ids); $i++) {
    $str=$str."{\"id\":\"".$ids[$i]."\",\"db\":\"".$dbs[$i]."\",\"name\":\"".$names[$i].
              "\",\"variety\":\"".$variety[$i]."\",\"grp\":\"".$sub_variety[$i].
              "\",\"origin\":\"".$origins[$i]."\",\"origin_2\":\"".$origins_2[$i]."\",\"type\":\"".$type[$i]."\"}"; 
    if ( $i<count($ids)-1 ) {
      $str=$str.",";
    }
  }
  $str=$str."]|[";

  ////////////////////////////////////////////////////////// List of tracks
  $queryT = "SELECT DISTINCT track FROM sys_track order by seq";
  $resultT = mysql_query($queryT);
  $tracks = array();
  while ($rowT = mysql_fetch_row($resultT)) {
    $tracks[]=$rowT[0];
  }

  for ($i=0; $i<count($tracks); $i++) {
    $queryC = "SELECT count(*) FROM sys_track WHERE parent=0 AND track='$tracks[$i]'";
    $resultC = mysql_query($queryC);
    $rowC = mysql_fetch_row($resultC);
    $color = "#000000";
    if ( $rowC[0] > 0 ) {
      $color = "#000080";
    }
    $str=$str."{\"track\":\"".$tracks[$i]."\",\"p\":\"1\",\"h\":\"4\",\"c\":\"".$color."\",\"s\":\"".$i."\"}";
    if ( $i<count($tracks)-1 ) {
      $str=$str.",";
    }
  }
  $str=$str."]";

  echo $str;

?>

