You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Sebastian Sulinski的高级搜索表单实现筛选结果分页?

Adding Pagination to Sebastian Sulinski's Advanced Search Form

Let's walk through how to properly add pagination to this search form, since you mentioned your existing pagination class isn't working. I'm familiar with this tutorial project, so I'll break it down step by step:

1. Fix the Core Query Logic

The key to pagination is calculating two critical values: total matching records (to determine total pages) and limiting results to the current page. Here's how to adjust the backend search logic (likely in search.php or a similar file):

Step 1a: Calculate Total Matching Records

First, run a count query using the exact same filter conditions as your main search. This ensures you're counting only the records that match the user's selections:

// Base search query and params built from form filters
$sql = "SELECT * FROM products WHERE 1=1";
$params = [];

// Add your existing filter logic here (e.g., keyword, category, price range)
if (!empty($_GET['keyword'])) {
    $sql .= " AND name LIKE ?";
    $params[] = "%{$_GET['keyword']}%";
}
// ... add other filter conditions like category, price, etc. ...

// Create count query (replace SELECT * with SELECT COUNT(*))
$countSql = preg_replace("/SELECT .* FROM/", "SELECT COUNT(*) FROM", $sql);
$stmt = $pdo->prepare($countSql);
$stmt->execute($params);
$totalRecords = (int)$stmt->fetchColumn();

Step 1b: Add Pagination to the Main Query

Define your pagination settings, calculate the offset, and modify the main query to limit results:

// Pagination configuration
$perPage = 10; // Adjust to your preferred results per page
$currentPage = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] > 0 
    ? (int)$_GET['page'] 
    : 1;
$offset = ($currentPage - 1) * $perPage;

// Add LIMIT/OFFSET to the main search query
$sql .= " LIMIT ? OFFSET ?";
$params[] = $perPage;
$params[] = $offset;

// Fetch paginated results
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);

2. Fix Your Pagination Class Integration

The most common issue with pagination classes here is not preserving filter parameters in the page links. Here's a reliable pagination class (or adjust your existing one with this logic):

Example Pagination Class (Filter-Preserving)

class Paginator {
    private $totalRecords;
    private $perPage;
    private $currentPage;
    private $totalPages;

    public function __construct($totalRecords, $perPage, $currentPage) {
        $this->totalRecords = $totalRecords;
        $this->perPage = $perPage;
        $this->currentPage = $currentPage;
        $this->totalPages = ceil($totalRecords / $perPage);
    }

    public function renderLinks() {
        if ($this->totalPages <= 1) return "";

        $links = "<div class='pagination'>";
        // Keep all filter parameters except the current page
        $baseQuery = array_diff_key($_GET, ['page' => '']);
        $queryString = http_build_query($baseQuery);

        // Previous Page Link
        if ($this->currentPage > 1) {
            $prevUrl = $queryString ? "?{$queryString}&page=" . ($this->currentPage - 1) : "?page=" . ($this->currentPage - 1);
            $links .= "<a href='{$prevUrl}' class='pagination-link'>Previous</a>";
        }

        // Page Number Links
        for ($i = 1; $i <= $this->totalPages; $i++) {
            if ($i === $this->currentPage) {
                $links .= "<span class='pagination-current'>{$i}</span>";
            } else {
                $pageUrl = $queryString ? "?{$queryString}&page={$i}" : "?page={$i}";
                $links .= "<a href='{$pageUrl}' class='pagination-link'>{$i}</a>";
            }
        }

        // Next Page Link
        if ($this->currentPage < $this->totalPages) {
            $nextUrl = $queryString ? "?{$queryString}&page=" . ($this->currentPage + 1) : "?page=" . ($this->currentPage + 1);
            $links .= "<a href='{$nextUrl}' class='pagination-link'>Next</a>";
        }

        $links .= "</div>";
        return $links;
    }
}

In your backend, create the paginator instance and pass the HTML to your frontend template:

// Set up the paginator
$paginator = new Paginator($totalRecords, $perPage, $currentPage);
$paginationHtml = $paginator->renderLinks();

Then in your HTML template (after displaying search results), add the pagination:

<!-- Insert after your results list -->
<?php echo $paginationHtml; ?>

3. Troubleshooting Common Issues

If pagination still isn't working, check these common pitfalls:

  • Mismatched count query: Ensure the COUNT(*) query uses the exact same WHERE clauses (and joins, if any) as your main search. A mismatch will give incorrect total pages.
  • Missing filter parameters: Click a pagination link and check the URL—does it include all filter values (e.g., ?keyword=laptop&category=electronics&page=2)? If not, your paginator isn't preserving the query string correctly.
  • Invalid page numbers: Sanitize the page parameter (convert to integer, set minimum 1) to avoid SQL errors or broken links.
  • Styling issues: If links aren't visible, check that your CSS targets the .pagination class correctly—sometimes the problem is visual, not functional.

内容的提问来源于stack exchange,提问作者Abdullah Alasham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:41:16