<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>LEIGH&amp;CO Awards </title>
    <link href="../../../css/bootstrap-4.4.1.css" rel="stylesheet">
    <link href="../../../css/leighco.css" rel="stylesheet">
</head>
<body>

<h1>LEIGH&CO Leaderboard</h1>
<br/>

<?php
$conn = new mysqli('db1.leigh-co.com', 'lc', 'PASSWORD', 'lc') or die($conn->error);
$q = "select first_name,last_name,label as award_label,count(*) as cnt from (select last_name, first_name, label, b.id from award_t a, person_t b, award_grant_t c where a.id = c.award_id and b.id = c.person_id) a group by id, label order by last_name,first_name,award_label";
$sql_query = $conn->prepare($q) or die($conn->error);
$sql_query->execute() or die($conn->error);
$result = $sql_query->get_result();
$item = $result->fetch_all(MYSQLI_ASSOC);

$last = "";
foreach ($item as $row) {
    $fullName = $row['last_name'] . ', ' . $row['first_name'] . '<br>';
    if ($fullName !== $last) {
        print("<p/><h3>$fullName</h3>");
    }
    $last = $fullName;
    print($row[cnt] . 'x ' . $row[award_label] . '<br>');
}
?>
</body>
</html>