1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <?php
include 'connexion.php';
// code html tableau
$html_header_question = '';
$html_header_coef = '';
$html_body = '';
$ids_question = [];
// création du code html de l'en-tête
foreach ($connection->query('SELECT id, coef FROM coeffi ORDER BY id', PDO::FETCH_ASSOC) as $v) {
$html_header_question .= '<th>'.(int)$v['id'].'</th>';
$html_header_coef .= '<th>'.(int)$v['coef'].'</th>';
$ids_question[] = (int)$v['id'];
}
// fonction d'échappement des caractères dangereux
$hsc = function($p) { return htmlspecialchars($p, ENT_QUOTE, 'utf8'); };
// résultats du questionnaire
$sql = <<<'sql'
SELECT username, inscription_id, titre_id, point_total
FROM reponses r
INNER JOIN inscriptions i ON i.id = r.inscription_id
INNER JOIN coeffi c ON c.id = r.titre_id
ORDER BY username, titre_id
sql;
$last_user_id = '';
$answers = [];
// vu qu'il n'y a aucun paramèrtre, la préparation de la requête est inutile
foreach ($connection->query($sql, PDO::FETCH_ASSOC) as $v) {
// gestion des nouveaux noms
if ($last_user_id !== (int)$v['inscription_id']) {
$answers[$v['inscription_id']] = array_fill_keys($ids_question, '');
$answers[$v['inscription_id']]['username'] = $v['username'];
$last_user_id = $v['inscription_id'];
}
$answers[$last_user_id][(int)$v['titre_id']] = (int)$v['point_total'];
}
foreach ($answers as $user_id => $v) {
$html_body .= '<tr><td>'.$hsc(array_pop($v)).'</td>'; // username
if ( ! empty($v)) {
$html_body = '<td>'.implode('</td></td>', $v).'</td>';
}
$html_body .= '</tr>';
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Résultats</title>
<meta charset="utf-8" /><!--Pour obtenir les bons caractères-->
</head>
<body>
<table>
<thead>
<tr><th>Question n°</th><?= $html_header_question ?></tr>
<tr><th>Coefficient</th><?= $html_header_coef ?></tr>
</thead>
<tbody>
<?= $html_body ?>
</tbody>
</table>
</body>
</html> |