<?php
session_start();
$tag = $_GET["r"];
$qid = $_GET["q"];
$questions = json_decode( file_get_contents( 'data/questions.json' ), true );
?>
<html>
<head>
<title> The Learning Jar </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css" />
<meta content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name = "viewport" />
</head>
<body>
<div id=container>
	<div id=top-logo>
	<img src=img/jar_s.png width=50>

	<a href=index.php>
	<img src=img/logo_s.png width=250>
	</a>
	</center>
	</div>

	
	<div id=content>
	<h1>
	<?php
		if ($tag != "" ) echo $tag;
		if ( $qid > 0 ) {
			echo $questions[($qid-1)]["title"];
		}
	?>
	</h1>
	
	<table width=100% style="font-size:12">
	<?php
		$answers = json_decode( file_get_contents( 'data/answers.json' ), true );
		$mycount = 0;
		foreach($questions as $q) {
			$tags = explode(",", $q["tags"]);
			$exist = False;
			foreach($tags as $etag) {
				if ($etag == $tag) $exist = True;
			}
			if ( $q["id"] == $qid-1) $exist = True;

			if ( $exist == False ) continue;

				echo "<tr bgcolor=#BBF>";
			echo "<td width=200>".$q["title"]."</td><td>";
			foreach($tags as $etag) {
				echo $etag.", ";
			}
			echo "</td>";
			echo "</tr>";
			$acount = 0;
			foreach($answers as $a) {
				if ( $a["qid"] == $q["id"] ) {
					$acount += 1;
					if ($mycount % 2 == 1 ) {
						echo "<tr bgcolor=#DDD>";
					} else {
						echo "<tr>";
					}

					echo "<td> -- <a href=ansview.php?r=".$a["id"].">"
					.$a["username"]."</a></td><td>"
					."<font color=#5F5>Like:".($a["up"]==0?"0":$a["up"])."</font>";
					//."/<font color=#F88>".($a["down"]==0?"0":$a["down"])."</font></td>";
					echo "</tr>";
					$mycount += 1;
				}
			}
			if ( $acount == 0 ) {
				echo "<tr><td>No answers yet!</td></tr>";
			}
		}
	?>
	</table>
	
	</div>
</div>
</body>
</html>
