Fix pagination styling, add-to-cart class/data-attr mismatch, shop.php pagination

This commit is contained in:
2026-06-14 16:24:39 +00:00
parent da60888b72
commit 548713971d
2 changed files with 41 additions and 29 deletions
+39 -20
View File
@@ -204,29 +204,48 @@ function paginate($totalItems, $currentPage, $perPage = ITEMS_PER_PAGE) {
*/
function renderPagination($pagination, $baseUrl) {
if ($pagination['total_pages'] <= 1) return '';
$html = '<nav class="pagination"><ul>';
// Previous
if ($pagination['has_prev']) {
$html .= '<li><a href="' . $baseUrl . '?page=' . ($pagination['current_page'] - 1) . '">&laquo; Previous</a></li>';
}
// Page numbers
for ($i = 1; $i <= $pagination['total_pages']; $i++) {
if ($i == $pagination['current_page']) {
$html .= '<li class="active"><span>' . $i . '</span></li>';
} else {
$html .= '<li><a href="' . $baseUrl . '?page=' . $i . '">' . $i . '</a></li>';
$cur = $pagination['current_page'];
$total = $pagination['total_pages'];
// baseUrl may already contain query params; append page with &
$sep = strpos($baseUrl, '?') !== false ? '&' : '?';
$url = fn($p) => $baseUrl . $sep . 'page=' . $p;
$btn = fn($href, $label, $active = false, $disabled = false) =>
'<a href="' . ($disabled ? '#' : htmlspecialchars($href)) . '" style="'
. 'display:inline-flex;align-items:center;justify-content:center;'
. 'min-width:36px;height:36px;padding:0 10px;border-radius:6px;'
. 'font-size:.875rem;font-weight:500;text-decoration:none;transition:all .15s;'
. ($active ? 'background:#FF5E1A;color:#fff;cursor:default;' :
($disabled ? 'background:transparent;color:#444;cursor:default;pointer-events:none;' :
'background:#1e1e1e;color:#ccc;border:1px solid #333;'))
. '">' . $label . '</a>';
$html = '<div style="display:flex;align-items:center;justify-content:center;gap:4px;padding:1.5rem 0;flex-wrap:wrap">';
// Prev
$html .= $btn($url($cur - 1), '&#8592; Prev', false, !$pagination['has_prev']);
// Page numbers with ellipsis
$pages = [];
for ($i = 1; $i <= $total; $i++) {
if ($i === 1 || $i === $total || abs($i - $cur) <= 2) {
$pages[] = $i;
}
}
// Next
if ($pagination['has_next']) {
$html .= '<li><a href="' . $baseUrl . '?page=' . ($pagination['current_page'] + 1) . '">Next &raquo;</a></li>';
$prev = null;
foreach ($pages as $p) {
if ($prev !== null && $p - $prev > 1) {
$html .= '<span style="color:#555;padding:0 4px">…</span>';
}
$html .= $btn($url($p), $p, $p === $cur);
$prev = $p;
}
$html .= '</ul></nav>';
// Next
$html .= $btn($url($cur + 1), 'Next &#8594;', false, !$pagination['has_next']);
$html .= '</div>';
return $html;
}
+2 -9
View File
@@ -144,7 +144,7 @@ $productTypesList = db()->fetchAll("SELECT type_id, name, slug FROM product_type
<span class="original"><?= formatCurrency($product['price']) ?></span>
<?php endif; ?>
</div>
<button class="btn btn-primary btn-block add-to-cart" data-id="<?= $product['product_id'] ?>">
<button class="btn btn-primary btn-block add-to-cart-btn" data-product-id="<?= $product['product_id'] ?>">
<i class="fas fa-shopping-bag"></i> Add to Cart
</button>
</div>
@@ -154,14 +154,7 @@ $productTypesList = db()->fetchAll("SELECT type_id, name, slug FROM product_type
<!-- Pagination -->
<?php if ($pagination['total_pages'] > 1): ?>
<div style="display: flex; justify-content: center; gap: 0.5rem; margin-top: 3rem;">
<?php for ($i = 1; $i <= $pagination['total_pages']; $i++): ?>
<a href="/shop.php?page=<?= $i ?><?= $category ? '&category=' . urlencode($category) : '' ?><?= $sort ? '&sort=' . $sort : '' ?>"
class="btn <?= $i === $page ? 'btn-primary' : 'btn-secondary' ?>">
<?= $i ?>
</a>
<?php endfor; ?>
</div>
<?= renderPagination($pagination, '/shop.php?' . http_build_query(array_filter(['category' => $category, 'sort' => $sort]))) ?>
<?php endif; ?>
<?php endif; ?>
</div>