<?php  

$sqldata = "SELECT * FROM questions WHERE id_grille = '" . $_SESSION['id_grille'] . "' AND id_patient = '" . $_SESSION['id_patient'] . "' AND date = DATE(NOW())";
$reqdata = cleanPDOQuery($sqldata);
$total = $reqdata->rowCount();

	if ($total != 0) {
		$k = 0;
		while($resdata = $reqdata->fetch())
		{
			$reponse[$k] = $resdata['reponse'];
			$id_question[$k] = $resdata['id_question'];
			$k++;
		}

?>

<form name="formQ" style="color: black; margin-left: 40%;">
	<table>
		<tr><th style="width:30%;">Questions</th><th>Oui</th><th>Non</th></tr>
		<?php
			$trueOrFalse = -1;
			for($i = 0; $i < $total; $i++)
			{
				if ($reponse[$i] == 1) {
					$trueOrFalse = 0;
				}
				else if ($reponse[$i] == 0) {
					$trueOrFalse = 1;
				}
			
				echo '<tr><td>Question ' . $id_question[$i] . '</td>';
				for($j = 0; $j < 2; $j++)
				{
					echo '<td><input type="radio" name="radio2_' . $id_question[$i] . '" onchange="updateQuestions(' . $id_question[$i] . ', "ON");"';
					if($trueOrFalse == $j) { echo 'checked';}
					echo '/></td>';
					if ($j == 2) { echo '</tr>'; }
				}
			}
			
		?>
	</table>
</form>
<?php
	}
	else {
	?>
		<form name="formQ" style="color: black; margin-left: 40%;">
			<table>
				<tr><th style="width:30%;">Questions</th><th>Oui</th><th>Non</th></tr>
				<tr><td>Question 1</td><td><input type="radio" name="radio2_1" onchange="updateQuestions(1, 'ON');" /></td><td><input type="radio" name="radio2_1" onchange="updateQuestions(1, 'ON');" /></td></tr>
				<tr><td>Question 2</td><td><input type="radio" name="radio2_2" onchange="updateQuestions(2, 'ON');" /></td><td><input type="radio" name="radio2_2" onchange="updateQuestions(2, 'ON');" /></td></tr>
				<tr><td>Question 3</td><td><input type="radio" name="radio2_3" onchange="updateQuestions(3, 'ON');" /></td><td><input type="radio" name="radio2_3" onchange="updateQuestions(3, 'ON');" /></td></tr>
			</table>
		</form>
	<?php
	}
	?>