PHP PDO与搜索分页

问题描述 投票:1回答:1

我知道这个问题真的被问过,但我STIL跑进麻烦,即使有很多的材料阅读。我的问题是,我真的不能处理的同时搜索和分页,因为他们对自己的正常工作。脚本的这个地区是主要的逻辑是:

// Quantity of results per page
$limit = 5;

// Check if page has been clicked
if (!isset($_GET['page'])) {
    $page = 1;
} else{
    $page = $_GET['page'];
}

// Initial offset, if page number wasn't specified than start from the very beginning
$starting_limit = ($page-1)*$limit;

// Check if search form has been submitted
if (isset($_GET['search'])) {
    $searchTerm = $_GET['search'];
    $sql = "SELECT company.id, company.name, inn, ceo, city, phone
              FROM company
              LEFT JOIN address ON company.id = address.company_id
              LEFT JOIN contact ON company.id = contact.company_id
              WHERE
                MATCH (company.name, inn, ceo) AGAINST (:searchTerm)
                OR MATCH (city, street) AGAINST (:searchTerm)
                OR MATCH(contact.name, phone, email) AGAINST (:searchTerm)
                ORDER BY id DESC LIMIT $starting_limit, $limit";
    $stmt = $pdo->prepare($sql);
    $stmt->execute(array(':searchTerm' => $searchTerm));
    // Count number of rows to make proper number of pages
    $total_results = $stmt->rowCount();
    $total_pages = ceil($total_results/$limit);
} else { // Basically else clause is similar to the search block except no search is being made
    $sql = "SELECT * FROM company";
    $stmt = $pdo->prepare($sql);
    $stmt->execute();

    // Again count number of rows
    $total_results = $stmt->rowCount();
    $total_pages = ceil($total_results/$limit);
    // And then make a query
    $stmt = $pdo->prepare("
      SELECT company.id, company.name, company.inn,
            company.ceo, address.city, contact.phone
          FROM company
          LEFT JOIN address
          ON company.id = address.company_id
          LEFT JOIN contact
          ON company.id = contact.company_id
          ORDER BY id ASC LIMIT $starting_limit, $limit");
    $stmt->execute();
}
?>

这是非常不言自明(填充表返回的数据):

<?php
       // Filling the result table with results
       while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
           echo "<tr><td scope='row'>" . $row['inn'] . "</td>";
           echo "<td><a href='company.php?id=" . $row["id"] . "'>" . $row['name'] . "</a>" . "</td>";
           echo "<td>" . $row['ceo'] . "</td>";
           echo "<td>" . $row['city'] . "</td>";
           echo "<td>" . $row['phone'] . "</td></tr>";
       }
       ?>

这里是分页面板:

<?php
    // Debug information - test how many results and pages there actually were
    echo $total_results."\n";
    echo $total_pages;

    // Paginating part itself
    for ($page=1; $page <= $total_pages ; $page++):?>

    <a href='<?php
        if (isset($searchTerm)) {
            echo "pagination_test.php?search=$searchTerm&page=$page";
        } else {
            echo "pagination_test.php?page=$page";
        } ?>' class="links"><?php  echo $page; ?>
    </a>

    <?php endfor; ?>

这里的问题是,虽然分页本身完美的作品,以及搜索,但是当我结合搜索和页面参数,我回来只有5个记录永远任何搜索查询和分页只有1页,但我仍然能手动去一个网页和resulsts在那里,正确的!

请帮我找出问题所在。不仅如此,我开始注意到,代码变得相当草率和unmaintanable,我对事业欢迎任何批评和代码组织/结构的意见,我知道什么是错的这一个:)

php pdo pagination
1个回答
2
投票

你需要编写单独的查询,通过搜索标准进行限制,以获得结果的总数

$sql = "SELECT company.id, company.name, inn, ceo, city, phone
          FROM company
          LEFT JOIN address ON company.id = address.company_id
          LEFT JOIN contact ON company.id = contact.company_id
          WHERE
            MATCH (company.name, inn, ceo) AGAINST (:searchTerm)
            OR MATCH (city, street) AGAINST (:searchTerm)
            OR MATCH(contact.name, phone, email) AGAINST (:searchTerm)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':searchTerm' => $searchTerm));

$total_results_without_limit = $stmt->rowCount();
$total_pages = ceil($total_results_without_limit/$limit);
© www.soinside.com 2019 - 2024. All rights reserved.