在PHP中搜索后分页

时间:2019-05-25 13:07:56

标签: php sql ajax

我有一个搜索按钮,用于搜索类别和产品表。 因此,当页面在第一个下拉列表中加载时显示类别时,我使用2个下拉列表;当用户在第二个下拉列表中选择类别时,则显示该类别中的产品。同样,当页面加载时,运行默认查询,该查询显示所有产品。因此搜索功能运行良好。

问题是我也在使用分页。在搜索之前,分页工作正常(每页15条记录),但是在搜索后,如果我选择一个类别并进行搜索,它将显示该类别的所有记录,当我进入下一页时,它将运行默认查询并显示默认结果。我希望它显示我搜索的结果。

<?php 
            $listperpage = 15;
            $c = $_POST['category'];
            $n = $_POST['product'];
            if(!empty($c)){
                $query = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = '$c'";
                $result = mysql_query($query) or die (mysql_query());
            }
            else if(!empty($c) && !empty($n)){
                $query = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = '$c' AND rank_name = '$n'";
                $result = mysql_query($query) or die (mysql_query());
            }else{
                $query = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category";
                $result = mysql_query($query) or die (mysql_query());
                echo "else one";
            }

            $result_num = mysql_num_rows($result);
            //total pages
            $pages = ceil($result_num/$listperpage);
            //which number the user is crrrently on 
            if (!isset($_GET['page'])) {
                $page = 1;
            } else{
                $page = $_GET['page'];
            }
            //sql limit starting number of results on th page
            $strt_limit_no = ($page - 1)*$listperpage;

            if(isset($_POST["search"])){
                $c = $_POST['category'];
                $n = $_POST['product'];

                if(empty($c)){
                    echo "<span class='form-error'>Select Category first!</span>";
                }
                else if(!empty($c) && !empty($n)){
                    $queryn = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = '$c' AND rank_name = '$n' LIMIT ". $strt_limit_no .','. $listperpage;
                    $results =  mysql_query($queryn) or die (mysql_query());
                }
                else{
                    $queryn = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = '$c' LIMIT ". $strt_limit_no .','. $listperpage;
                    $results =  mysql_query($queryn) or die (mysql_query());
                }
            }
            else{
                $queryn = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category LIMIT ". $strt_limit_no .','. $listperpage;
                $results = mysql_query($queryn) or die (mysql_query());
                echo "else end";
            }
        ?>
<script type="text/javascript">
    $(document).ready(function() {
        $("#category").on('click',function(){
            var category = $(this).val();
            if(category){
                //alert('3213');
                $.ajax({
                    type:'POST',
                    url:'process.php',
                    data:'category=' + category,
                    success:function(html){
                        $("#product").html(html);
                    }
                });
            }
        });
    });
</script>
<div align="center" class='maintabledivdaily' style='width:100%; padding: 20px 0 15px 0;' >
        <table border="1" cellpadding="5" cellspacing="2" width="80%" >
            <thead>
                <tr>
                    <th colspan="9" bgcolor="#CCCCCC">List of all Ranks</th>
                </tr>
                <tr>
                    <th>ID</th>
                    <th>Category</th>
                    <th>Product Name</th>
                </tr>
            </thead>
            <tbody>
                <?php while ($row = mysql_fetch_array($results)): ?>
                <tr></tr>
                <tr align="center" dir="rtl" style="font-style:">
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo $row['category']; ?></td>
                    <td><?php echo $row['product']; ?></td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
        <br>
        <div class="pagination">
            <?php
            for ($page=1; $page <= $pages ; $page++) { 
                echo '<a style="color: #000000;
                        float: left;
                        padding: 8px 16px;
                        text-decoration: none;
                        "href="list.php?page='. $page .'">'. $page .'</a> ';
            }
            ?>
        </div>

1 个答案:

答案 0 :(得分:3)

从发布方式更改为获取方式,您可以在其中选择类别和搜索按钮。

<form method="get"></form>

获取服务器端的类别和其他发布的值。

$c = $_GET['category'];
$n = $_GET['product'];

将这些值添加到分页中

<div class="pagination">
        <?php
        $cat = $prod = '';
        if(!empty($_GET['category'])){ $cat = '&category='.$_GET['category']; }
        if(!empty($_GET['product'])){ $prod = '&product='.$_GET['category']; }
        for ($page=1; $page <= $pages ; $page++) { 
            echo '<a style="color: #000000;
                    float: left;
                    padding: 8px 16px;
                    text-decoration: none;
                    "href="list.php?page='. $page. $cat. $prod .'">'. $page .'</a> ';
        }
        ?>
    </div>

注意:如果搜索时未发送'page'参数,则在搜索表单中设置一个名为page的输入。

<input type="hidden" name="page" value="<?php echo $page; ?>"

第二种方法是,您可以将发布的值存储在SESSION中,并在单击新按钮清除搜索后清除会话。

第1步:在“搜索”按钮旁边添加一个新按钮(“清除搜索”)。

第2步:将发布的类别和产品存储到会话中,并在查询中使用此会话。并通过清除搜索按钮销毁会话。

        <?php
        $listperpage = 15;
        $_SESSION['category'] = $_POST['category'];
        $_SESSION['product'] = $_POST['product'];
        /** clear_search is the name of your clear search button */
        if(isset($_POST["clear_search"])){
            unset($_SESSION['category']);
            unset($_SESSION['product']);
        }
        if(!empty($_SESSION['category'])){
            $query = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = ". $_SESSION['category'];
            $result = mysql_query($query) or die (mysql_query());
        }
        else if(!empty($_SESSION['category']) && !empty($_SESSION['product'])){
            $query = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = " .$_SESSION['category']. " AND rank_name = ". $_SESSION['product'];
            $result = mysql_query($query) or die (mysql_query());
        }else{
            $query = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category";
            $result = mysql_query($query) or die (mysql_query());
            echo "else one";
        }

        $result_num = mysql_num_rows($result);
        //total pages
        $pages = ceil($result_num/$listperpage);
        //which number the user is crrrently on 
        if (!isset($_GET['page'])) {
            $page = 1;
        } else{
            $page = $_GET['page'];
        }
        //sql limit starting number of results on th page
        $strt_limit_no = ($page - 1)*$listperpage;

        if(isset($_POST["search"])){
            if(empty($_SESSION['category'])){
                echo "<span class='form-error'>Select Category first!</span>";
            }
            else if(!empty($_SESSION['category']) && !empty($_SESSION['product'])){
                $queryn = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = " .$_SESSION['category']. " AND rank_name = " . $_SESSION['product'] . " LIMIT ". $strt_limit_no .','. $listperpage;
                $results =  mysql_query($queryn) or die (mysql_query());
            }
            else{
                $queryn = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category WHERE fk_id_category = " .$_SESSION['category']. " LIMIT ". $strt_limit_no .','. $listperpage;
                $results =  mysql_query($queryn) or die (mysql_query());
            }
        }
        else{
            $queryn = "SELECT * FROM tbl_category INNER JOIN tbl_product ON tbl_category.id = tbl_product.fk_id_category LIMIT ". $strt_limit_no .','. $listperpage;
            $results = mysql_query($queryn) or die (mysql_query());
            echo "else end";
        }
    ?>