简体   繁体   English

使用分页搜索PHP / MySQL

[英]Search PHP/MySQL with Pagination

I wrote this code to search in rows of the table: 我编写了以下代码来搜索表的行:

<div class="container mx-auto">
    <!--Add class table-responsive for responsive table -->
    <div class="row">
        <div class="col-md-6">
            <form method="post">
            <div class="input-group">
                <input type="text" class="form-control" placeholder="Search for..." name="searchValue">
                <span class="input-group-btn">
                    <button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
                </span>
            </div>
            </form>
        </div>
    </div>

    <table class="table mx-auto" id="'table">
        <thead>
        <tr>
            <th>Name</th>
            <th>Surname</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Company</th>
            <th>More</th>

        </tr>
        </thead>
        <tbody>
        <?php
        $page_max = 8;
        if (isset($_POST['search'])){
            $search = $_POST['searchValue'];

            $entriesInDatabase = $database->getData("SELECT count(id) FROM customers WHERE name LIKE '%$search%'");
            $numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
            $numberOfRecords = $page_max;
            if(isset($_GET['page'])) {
                $page = $_GET['page'];
                $start = $page * $page_max;
            }
            else{
                $page = 0;
                $start = $page * $page_max;

            }
            $customers = $database->getUsers("SELECT * FROM customers WHERE name LIKE '%$search%' LIMIT $start, $numberOfRecords");
        }
        else{
            $entriesInDatabase = $database->getData("SELECT count(id) FROM customers");
            $numberOfPages = ceil($entriesInDatabase['count(id)']/$page_max);
            $numberOfRecords = $page_max;
            if(isset($_GET['page'])) {
                $page = $_GET['page'];
                $start = $page * $page_max;
            }
            else{
                $page = 0;
                $start = $page * $page_max;

            }
            $customers = $database->getUsers("SELECT * FROM customers LIMIT $start, $numberOfRecords");
        }





        foreach($customers as $customer){
            $name = $customer ['name'];
            $surname = $customer['surname'];
            $email = $customer['email'];
            $phone = $customer['phone'];
            $company = $customer['company'];
            $id = $customer['id'];
            echo "<tr>
                    <td>$name</td>
                    <td>$surname</td>
                    <td>$email</td>
                    <td>$phone</td>
                    <td>$company</td>
                    <td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>



                  </tr>";
        }
        ?>
</tbody>
</table>
    <ul class="pagination">
<?php

if(isset($_GET['page']) && $_GET['page'] > 0){
    $previous = $_GET['page'] - 1;
    echo '<li class="page-item"><a class="page-link" href="?page='. $previous.'">Previous</a></li>';
}


for($i = 0; $i < $numberOfPages; $i++){
    echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
    if(isset($_GET['page']) && $_GET['page'] < $numberOfPages - 1){
        $page = $_GET['page'];
        $next = $page + 1;
        echo '<li class="page-item"><a class="page-link" href="?page='.$next.'">Next</a></li> ';
    }
    elseif(!isset($_GET['page'])){
        echo '<li class="page-item"><a class="page-link" href="?page=1">Next</a></li> ';
    }
?>



    </ul>
</div>

Searching works fine when I'm on page 0, but when I'm at page 2 I can only search for records on that page (I think this is because of the limit in my query) besides that when I search for 'A' it returns 7 pages but if I click on the seconde page it won't load the search results but the main list with customers on page 2. 当我在第0页上时,搜索工作正常,但是当我在第2页上时,除了搜索“ A”时,我只能在该页面上搜索记录(我认为这是由于查询的限制)它返回7页,但是如果我单击第二页,它将不会加载搜索结果,但会加载第2页上的客户主列表。

Could anyone help me fix these issues? 谁能帮我解决这些问题?

First of all: sanitize your input. 首先:清理您的输入。 Using POST or GET in database queries can be very, very dangerous. 在数据库查询中使用POST或GET可能非常非常危险。

Second: DRY , you probably can do this with half the code you are using. 第二: DRY ,您可能只用一半的代码即可完成此操作。

The reason why you are "losing" your search is because your pagination doesn't carry over the search term. 之所以“失去”搜索,是因为您的分页不会覆盖搜索字词。

Add it to the links in the pagination and switch from POST to GET: 将其添加到分页中的链接,然后从POST切换到GET:

<?php
$searchString = '';
if($_GET['searchValue']){
    $searchString = '&searchValue=' . $_GET['searchValue'];
} 

if(isset($_GET['page']) && $_GET['page'] > 0){
    $previous = $_GET['page'] - 1;
echo '<li class="page-item"><a class="page-link" href="?page='. $previous.$searchString.'">Previous</a></li>';
}
// more code
'... href="?page='.$i.$searchString'" ...'

'... href="?page='.$next.$searchString'" ...'

Note that I use $_GET['searchValue'] instead of POST. 请注意,我使用$_GET['searchValue']而不是POST。 in most cases there is no advantage in using POST for searches (quite the opposite, if you use GET you can track it in Google Analytics f.ex.). 在大多数情况下,使用POST进行搜索没有任何优势(相反,如果您使用GET,则可以在Google Analytics(分析)f.ex中进行跟踪)。 So make sure you change your form method to get and if (isset($_POST['search'])){ to $_GET['searchValue'] . 因此,请确保将表单方法更改为get和if (isset($_POST['search'])){改为$_GET['searchValue'] If you insist on using POST, you can use $_REQUEST , which holds values from GET and POST. 如果您坚持使用POST,则可以使用$_REQUEST ,其中包含GET和POST中的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM