<?php
session_start();

/* parametre de la connection au serveur mysql */
$server = 'localhost';
$user   = 'root';
$db     = 'test';

?>

<!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=ISO-8859-1" />
    <link rel="stylesheet" type="text/css" href="table.css" title="Normal" />
    <title>Tp 12</title>
</head>
<body>

<h1> Quizz </h1>

<form action="tp12.php" method=get>

<?php

/* connection au serveur mysql */
	$connexion = mysql_pconnect('localhost','root');
	if (!$connexion) { echo "Pas de connexion au serveur mySQL !"; exit; }
	if (!mysql_select_db($db, $connexion)) { echo "Base de donnees inexistante !"; exit; }

if (!isset($_REQUEST["ok"])) {
	$req = "SELECT * FROM departement";
	$res = mysql_query($req, $connexion);

	$index = rand(0,mysql_num_rows($res)-1);
	mysql_data_seek($res,$index);
	$ligne = mysql_fetch_array($res);
	$departement = $ligne["nom"];
	$_SESSION["nom"] = $ligne["nom"];
	$_SESSION["numero"] = $ligne["numero"];
?>
Rentrez le numro du dpartement suivant: <br />
<?php echo $departement; ?> ?
<input type="text" width="2" name="reponse" />
<input type="submit" name="ok" value="valider"/>

<?php 
} else {
	if ((isset($_SESSION["numero"]))&&($_SESSION["numero"]==$_REQUEST["reponse"])) {
		echo "Bravo !";
	} else {
		echo "Desol, ce n'est pas a.<br />";
		echo "La rponse tait : {$_SESSION['nom']} ({$_SESSION['numero']})";
 	}	
?>
<br />
<input type="submit" name="again" value="encore" />
<?php		
}
?>
</form>

<h2> Selection par Lettre </h2>

<?php
	for ($i=0; $i<26; $i++) {
		$c = chr( ord('A') + $i);
		echo "<a href=\"?lettre=$c\"> $c </a> &nbsp;";
	}
	echo "<br /><br />";
	if (isset($_GET["lettre"])) {
		$c = $_GET["lettre"];
		$req = "SELECT nom FROM departement WHERE nom LIKE '$c%' ";
		$res = mysql_query($req, $connexion);
		while ($ligne = mysql_fetch_row($res)) {
			echo $ligne[0]."<br />";
		}

	}
?>

</body>
</html>
