<?php
    /**
     * PARAMETRES DE CONNEXION A LA BASE DE DONNEES
     */
$ip = 'localhost';
$user = 'phpapp';
$pass = 'myphp';
$database = 'universite';

    // Définition de la source des données pour PDO
$dsn = "mysql:host=$ip;dbname=$database;charset=utf8mb4";

    // Création de l'objet $dbh, de type PDO, qui est la ressource d'accès à la base
try {
    $dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
    die("Erreur de connexion : ".$e->getMessage()); 
}

// Il est vérifié que le domaine a bien été fourni à la page.
if($_GET['domain'] != null) {
    // écriture en amont de la requête 
    $query = 'SELECT mSigle, mNom, mDescription FROM matiere WHERE mDomaine = :domaine';
    // Préparation de la requête
    $stmtMatieres = $dbh->prepare($query);
    // Association de la variable au paramètre
    $stmtMatieres->bindParam(":domaine", $_GET['domain']);
    // Exécution de la requête
    $stmtMatieres->execute();
?>

<table>
    <thead>
        <td><b>Intitulé</b></td>
        <td><b>Description</b></td>
        <td></td>
        <td></td>
    </thead>

<?php
    // Les résultats de la requête sont parcourus un par un, sous forme d'un tableau associatif $row
    while ($row = $stmtMatieres->fetch()) {
?>

        <tr>
            <td><?=$row['mNom'] ?></td>
            <td><?=$row['mDescription'] ?></td>
            <td><a href="modifMatiere.php?id=<?=$row['mSigle']?>">Modifier</a></td>
            <td><a href="supprimeMatiere.php?id=<?=$row['mSigle']?>">Supprimer</a></td>
        </tr>
    
<?php
    }
?>

</table>

<?php
} 
else{
    header('Location : index.php');
}
?>
<br>
<br>
<a href="index.php">Retour à l'accueil</a>
<?php

    // Fermeture de la connexion
$stmtDomains = null;
$pdoStmt = null;
$dbh = null;


    // Petite aide au développement
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<h3>SUIVI DES VARIABLES POUR DEVELOPPEMENT : </h3>";
echo "<h4>GLOBALS : </h4>";
echo '<pre>';
print_r($GLOBALS);