Ryanhub - file viewer
filename: views/admin/stats.php
branch: main
back to repo
<?php
if (!isAdmin()) {
    http_response_code(403);
    require __DIR__ . '/../public/403.php';
    return;
}

$pageTitle = "Admin – Stats";

$res = $db->query("SELECT path, COUNT(*) AS c FROM visits GROUP BY path ORDER BY c DESC");
$byPath = $res->fetch_all(MYSQLI_ASSOC);

$res = $db->query("SELECT DATE(created_at) AS d, COUNT(*) AS c FROM visits WHERE created_at >= (CURRENT_TIMESTAMP - INTERVAL 14 DAY) GROUP BY DATE(created_at) ORDER BY d ASC");
$byDay = $res->fetch_all(MYSQLI_ASSOC);
?>

<section class="page-grid">
    <div class="card" data-animate-initial>
        <div class="muted" style="font-size: 11px; letter-spacing: 0.18em; text-transform: uppercase; margin-bottom: 10px;">
            Admin / Visits
        </div>
        <a href="<?= url('admin') ?>" class="pill" style="display: inline-flex; align-items: center; gap: 6px; font-size: 11px; margin-bottom: 10px;">
            ← Back to admin
        </a>
        <h1 style="font-family: 'Georgia', 'Times New Roman', serif; font-weight: 400; font-size: 24px; margin: 0 0 12px;">
            Traffic.
        </h1>
    </div>

    <div class="card" data-animate>
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 18px;">
            <div>
                <div class="muted" style="font-size: 11px; text-transform: uppercase; letter-spacing: 0.16em; margin-bottom: 8px;">
                    By page
                </div>
                <table style="width: 100%; border-collapse: collapse; font-size: 13px;">
                    <thead>
                    <tr class="muted">
                        <th style="text-align:left; padding: 6px 4px;">Path</th>
                        <th style="text-align:right; padding: 6px 4px;">Visits</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($byPath as $row): ?>
                        <tr style="border-top: 1px solid rgba(0,0,0,0.04);">
                            <td style="padding: 6px 4px;"><?= htmlspecialchars($row['path'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding: 6px 4px; text-align:right;"><?= (int)$row['c'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div>
                <div class="muted" style="font-size: 11px; text-transform: uppercase; letter-spacing: 0.16em; margin-bottom: 8px;">
                    Last 14 days
                </div>
                <table style="width: 100%; border-collapse: collapse; font-size: 13px;">
                    <thead>
                    <tr class="muted">
                        <th style="text-align:left; padding: 6px 4px;">Date</th>
                        <th style="text-align:right; padding: 6px 4px;">Visits</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($byDay as $row): ?>
                        <tr style="border-top: 1px solid rgba(0,0,0,0.04);">
                            <td style="padding: 6px 4px;"><?= htmlspecialchars($row['d'], ENT_QUOTES, 'UTF-8') ?></td>
                            <td style="padding: 6px 4px; text-align:right;"><?= (int)$row['c'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</section>