<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  <style type="text/css">
table,td,th
{
border:1px solid black;
}
th
{
height:30px;
}
td
{
height:65px;
width:180px;
}
tr
{
height:65px;
}
</style>
  <link rel="stylesheet" href="stylus.css" />
  </head>
  <body>
  <?php
  if(!mysql_pconnect('localhost','root',''))
  die('<p>Server connection failed</p>');
  if(!mysql_select_db('bcdata'))
  die('<p>Database connection failed</p>');
  
  
  @mysql_query("SET CHARACTER SET utf8");
  @mysql_query("SET NAMES 'utf8'");
  
  
echo "<h2>ROZVRH HODÍN</h2>";


//select box pre nazov kurzu
$sql = "SELECT DISTINCT course_code, course_title_sk FROM course ORDER BY course_title_sk";
$v = mysql_query($sql);
echo "<form method='post' name='form_kurz'>";
echo "<select name='kurz' onchange='document.form_kurz.submit()'>\n";
echo "<option value=''>Názov kurzu</option>\n";

    while($r = mysql_fetch_array($v)){
          if($_POST['kurz'] == $r['course_code'])
          echo "<option value='".$r['course_code']."' selected='selected'>".$r['course_title_sk']." ".$r['course_code']."</option>\n";
          else
          echo "<option value='".$r['course_code']."'>".$r['course_title_sk']." ".$r['course_code']."</option>\n";
      }
echo "</select>\n";
echo "</form>";



//select box meno ucitela
$sqla= "SELECT DISTINCT person_fchpt_id,	person_fchpt_name,	person_fchpt_surname FROM course, person_fchpt
WHERE course_teachers = person_fchpt_id ORDER BY person_fchpt_surname";
$va = mysql_query($sqla);
echo "<form method='post' name='form_ucitel'>";
echo "<select name='ucitel' onchange='document.form_ucitel.submit()'>\n";
echo "<option value=''>Meno vyučujúceho</option>\n";

  while($ra = mysql_fetch_array($va)){
        if($_POST['ucitel'] == $ra['person_fchpt_id'])
        echo "<option value='".$ra['person_fchpt_id']."' selected='selected'>".$ra['person_fchpt_surname']." ".$ra['person_fchpt_name']."</option>\n";
        else
        echo "<option value='".$ra['person_fchpt_id']."'>".$ra['person_fchpt_surname']." ".$ra['person_fchpt_name']."</option>\n";
      }
echo "</select>\n";
echo "</form>";



//select box miestnost
$sqlb= "SELECT timetable_room FROM timetable GROUP BY timetable_room";
$vb = mysql_query($sqlb);
echo "<form method='post' name='form_miestnost'>";
echo "<select name='miestnost' onchange='document.form_miestnost.submit()'>\n";
echo "<option value=''>Miestnosť</option>\n";

      while($rb = mysql_fetch_array($vb)){
            if($_POST['miestnost'] == $rb['timetable_room'])
            echo "<option value='".$rb['timetable_room']."' selected='selected'>".$rb['timetable_room']."</option>\n";
            else
            echo "<option value='".$rb['timetable_room']."'>".$rb['timetable_room']."</option>\n";
            }
echo "</select>\n";
echo "</form>";


//select box kruzok
$sqlc= "CREATE FUNCTION SPLIT_STRING(
  x VARCHAR(255),
  delim VARCHAR(12),
  pos INT
)
RETURNS VARCHAR(255)
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(x, delim, pos),
       LENGTH(SUBSTRING_INDEX(x, delim, pos -1)) + 1),
       delim, '')";
       
mysql_query($sqlc);
$sqlc= "SELECT DISTINCT SPLIT_STRING(timetable_group,',',1) AS kruzok, timetable_class FROM timetable ORDER BY kruzok";
$vc = mysql_query($sqlc);
echo mysql_error();
echo "<form method='post' name='form_kruzok'>";
echo "<select name='kruzok' onchange='document.form_kruzok.submit()'>\n";
echo "<option value=''>Krúžok</option>\n";

    while($rc = mysql_fetch_array($vc)){
        if($_POST['kruzok'] == $rc['kruzok'])
        echo "<option value='".$rc['kruzok']."|".$rc['timetable_class']."' selected='selected'>".$rc['kruzok']." ".$rc['timetable_class']."</option>\n";
        else
        echo "<option value='".$rc['kruzok']."|".$rc['timetable_class']."'>".$rc['kruzok']." ".$rc['timetable_class']."</option>\n";
        }
echo "</select>\n";
echo "</form>";

?>

<form action="testrozvrh.php" method="POST">
<?php if(isset($_POST['miestnost'])) {
        if($_POST['miestnost'])
        {
          echo   "<input type='hidden' name='form_val' value='".$_POST['miestnost']."'>"   ;
          echo   "<input type='hidden' name='form_key' value='miestnost'>"   ;
         } 
      }
else if(isset($_POST['kruzok'])) {
  if($_POST['kruzok'])
      echo   "<input type='hidden' name='form_val' value='".$_POST['kruzok']."'>";
      echo   "<input type='hidden' name='form_key' value='kruzok'>";
   }    
else if(isset($_POST['kurz']))  {
  if($_POST['kurz'])
    echo   "<input type='hidden' name='form_val' value='".$_POST['kurz']."'>" ;
    echo   "<input type='hidden' name='form_key' value='kurz'>" ;
  }
else if(isset($_POST['ucitel'])) {
  if($_POST['ucitel'])
    echo   "<input type='hidden' name='form_val' value='".$_POST['ucitel']."'>" ;
    echo   "<input type='hidden' name='form_key' value='ucitel'>" ;
    }
?>
     
  <input type="submit" value="Export do PDF">
</form>

<?php

//rozvrh podla miestnosti
for($i=7;$i<=19;$i++)
{
for($j=1;$j<=6;$j++)
$t[$i][$j]="";
}
if(isset($_POST['miestnost'])){
  if($_POST['miestnost']){
    $sqlba="SELECT DISTINCT *,CONCAT(person_fchpt_name,' ',person_fchpt_surname) AS meno FROM timetable 
    LEFT JOIN course ON timetable_code=course_code
    LEFT JOIN person_fchpt ON timetable_lector=person_fchpt_id 
    WHERE timetable_room = '".$_POST['miestnost']."'";
    $vba = mysql_query($sqlba);
    while($rba = mysql_fetch_array($vba)){
      $t[intval($rba['timetable_start'])][$rba['timetable_day']]=$rba['course_title_sk'];
      $tx[intval($rba['timetable_start'])][$rba['timetable_day']]=$rba['meno'];
      $txa[intval($rba['timetable_start'])][$rba['timetable_day']]=$rba['timetable_group']; 
      $l=$rba['timetable_stop']-$rba['timetable_start'];
      $h[intval($rba['timetable_start'])][$rba['timetable_day']]=$l;
      for($i=1;$i<$l;$i++){
        $h[intval($rba['timetable_start'])+$i][$rba['timetable_day']]='x';
      }
    }
}

echo "<table border='1'>";
echo"<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  echo "<tr>\n";
  echo "<td>$i</td>\n";
  
  for($j=1;$j<=6;$j++)
  {
    if(!isset($h[$i][$j]))
    {
      echo "<td></td>\n";
  
    }
    elseif($h[$i][$j]=='x')
    {
      echo "";
  
    }
    elseif($h[$i][$j]>1)
    {
      echo "<td rowspan='".$h[$i][$j]."'>".$t[$i][$j]."<br />".$tx[$i][$j]."<br />".$txa[$i][$j]."</td>\n";
  
    } 
    else
      echo "<td> ".$t[$i][$j]."<br />".$tx[$i][$j]."<br />".$txa[$i][$j]."</td>\n";
  
  } 
  echo "</tr>\n";
  
}

echo "</table>";
}

//rozvrh podla kruzku
for($i=7;$i<=19;$i++)
{
for($j=1;$j<=6;$j++)
$tb[$i][$j]="";
}
if(isset($_POST['kruzok']))
{
if($_POST['kruzok']){
  $fdp = explode("|",$_POST['kruzok']);
  $sqlbb="SELECT DISTINCT *,CONCAT(person_fchpt_name,' ',person_fchpt_surname) AS meno FROM timetable
  LEFT JOIN course ON timetable_code=course_code
  LEFT JOIN person_fchpt ON timetable_lector=person_fchpt_id 
  WHERE timetable_group
  LIKE '%".$fdp[0]."%' AND timetable_class = '".$fdp[1]."'";
  $vbb = mysql_query($sqlbb);
  while($rbb = mysql_fetch_array($vbb)){
          if($fdp[0] == $rbb['timetable_group']){
          $tb[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$rbb['course_title_sk'];
          $txba[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$rbb['meno'];
          $txbb[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$rbb['timetable_room'];
          $lb=$rbb['timetable_stop']-$rbb['timetable_start'];
          $hb[intval($rbb['timetable_start'])][$rbb['timetable_day']]=$lb;
    for($i=1;$i<$lb;$i++){
                      $hb[intval($rbb['timetable_start'])+$i][$rbb['timetable_day']]='x';
                        }
          }}
}
echo "<table border='1'>";
echo"<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  echo "<tr>\n";
  echo "<td>$i</td>\n";
  for($j=1;$j<=6;$j++)
  {
    if(!isset($hb[$i][$j]))
    {
      echo "<td></td>\n";
    }
    elseif($hb[$i][$j]=='x')
    {
      echo "";
    }
    elseif($hb[$i][$j]>1)
    {
      echo "<td rowspan='".$hb[$i][$j]."'>".$tb[$i][$j]."<br />".$txba[$i][$j]."<br />".$txbb[$i][$j]."</td>\n";
    } 
    else
      echo "<td> ".$tb[$i][$j]."<br />".$txba[$i][$j]."<br />".$txbb[$i][$j]."</td>\n";
  } 
  echo "</tr>\n";
}
echo "</table>";

}


//rozvrh podla ucitela
for($i=7;$i<=19;$i++)
{
for($j=1;$j<=6;$j++)
$tc[$i][$j]="";
}
if(isset($_POST['ucitel']))
{
if($_POST['ucitel']){
    $sqlbc="SELECT DISTINCT *
    FROM timetable
    LEFT JOIN course ON timetable_code=course_code  
    WHERE timetable_lector = '".$_POST['ucitel']."'";
    $vbc = mysql_query($sqlbc);
    while($rbc = mysql_fetch_array($vbc)){
          $tc[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$rbc['course_title_sk'];
          $tca[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$rbc['timetable_group'];
          $tcb[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$rbc['timetable_room'];
          
          $lc=$rbc['timetable_stop']-$rbc['timetable_start'];
          $hc[intval($rbc['timetable_start'])][$rbc['timetable_day']]=$lc;
            for($i=1;$i<$lc;$i++){
                                  $hc[intval($rbc['timetable_start'])+$i][$rbc['timetable_day']]='x';
                                  }
          }
    }
echo "<table border='1'>";
echo"<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  echo "<tr>\n";
  echo "<td>$i</td>\n";
  for($j=1;$j<=6;$j++)
  {
    if(!isset($hc[$i][$j]))
    {
      echo "<td></td>\n";
    }
    elseif($hc[$i][$j]=='x')
    {
      echo "";
    }
    elseif($hc[$i][$j]>1)
    {
      echo "<td rowspan='".$hc[$i][$j]."'>".$tc[$i][$j]."<br />".$tca[$i][$j]."<br />".$tcb[$i][$j]."</td>\n";
    } 
    else
      echo "<td> ".$tc[$i][$j]."<br />".$tca[$i][$j]."<br />".$tcb[$i][$j]."</td>\n";
  } 
  echo "</tr>\n";
}
echo "</table>";

}


//rozvrh podla kurzu
for($i=7;$i<=19;$i++)
{
for($j=1;$j<=6;$j++)
$td[$i][$j]="";
}
if(isset($_POST['kurz']))
{
if($_POST['kurz']){ 
      $sqlbd="SELECT DISTINCT *,CONCAT(person_fchpt_name,' ',person_fchpt_surname) AS meno 
      FROM timetable 
      LEFT JOIN course ON timetable_code=course_code
      LEFT JOIN person_fchpt ON timetable_lector=person_fchpt_id  
      WHERE timetable_code = '".$_POST['kurz']."'";
      $vbd = mysql_query($sqlbd);
      while($rbd = mysql_fetch_array($vbd)){
            $td[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['course_title_sk'];
            $tda[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['meno'];
            $tdb[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['timetable_group'];
            $tdc[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$rbd['timetable_room'];
            
            $ld=$rbd['timetable_stop']-$rbd['timetable_start'];
            $hd[intval($rbd['timetable_start'])][$rbd['timetable_day']]=$ld;
            for($i=1;$i<$ld;$i++){
                                $hd[intval($rbd['timetable_start'])+$i][$rbd['timetable_day']]='x';
                                  }
            }
    }
    
echo "<table border='1'>";
echo"<tr><th></th><th>PONDELOK</th><th>UTOROK</th><th>STREDA</th><th>ŠTVRTOK</th><th>PIATOK</th><th>SOBOTA</th></tr>";
for($i=7;$i<=19;$i++)
{
  echo "<tr>\n";
  echo "<td>$i</td>\n";
  for($j=1;$j<=6;$j++)
  {
    if(!isset($hd[$i][$j]))
    {
      echo "<td></td>\n";
    }
    elseif($hd[$i][$j]=='x')
    {
      echo "";
    }
    elseif($hd[$i][$j]>1)
    {
      echo "<td rowspan='".$hd[$i][$j]."'>".$td[$i][$j]."<br />".$tda[$i][$j]."<br />".$tdb[$i][$j]."<br />".$tdc[$i][$j]."</td>\n";
    } 
    else
      echo "<td> ".$td[$i][$j]."<br />".$tda[$i][$j]."<br />".$tdb[$i][$j]."<br />".$tdc[$i][$j]."</td>\n";
  } 
  echo "</tr>\n";
}
echo "</table>";

}


?>
</body>
</html>