如何让分页保留搜索参数

时间:2019-02-22 15:32:54

标签: php pagination

最近,我正在为我的公司编写产品目录。我使用下拉列表为具有分页的网格视图产品列表提供过滤器。如果产品页数不超过1个,则过滤器可以正常工作。当搜索结果超过一页时,浏览器将显示所有产品,与未应用过滤器相同。下面是我的index.php代码。请帮忙,因为我已经在这里呆了一个星期。预先感谢。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php session_start();
if (!isset($_SESSION['user_name']))
{
echo "<script>window.open('../login.php','_self')</script>";
}  
$user_name=isset($_SESSION['user_name'])?$_SESSION['user_name']:'';
?>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="css/gallery.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-3.2.1.min.js"></script>
<meta name="theme-color" content="#444" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<?php 
include_once('../database/db_conection.php');//Include the database connection 
?>
<div class="container">
    <div class="col-md-2"> <br/>
    <a href="../main.php"> <i class="fa fa-home" style="font-size:28px"></i>Home</a> <br/>
    <br/>
    <br/>
        <form action="buckle.php'" method="post" >
        <div class="cf_header5">MATERIAL<br/>
        (e.g.Brass, Alloy...)</div>
        <select name="material" style="width:150px; height:25px;font-size:16px">
        <option value="">Please select ....</option>
        ';
        
        <?php
        $sql_ml = "SELECT distinct material FROM products where material !='' order by material";				
        $records_ml=mysqli_query($dbcon,$sql_ml);
        while ($row_ml=mysqli_fetch_array($records_ml)){
        echo '<option value="'.$row_ml['material'].'">'.$row_ml['material'].'</option>';
        }
        ?>
        </select>
        <br/>
        <br/>
        <div class="cf_header5">DESCRIPTION</div>
        <select name="description" style="width:150px; height:25px;font-size:16px">
        <option value="">Please select ....</option>
        ';
        
        <?php
        $sql_dn = "SELECT distinct catagory FROM products where catagory !='' order by catagory";
        $records_dn=mysqli_query($dbcon,$sql_dn);
        while ($row_dn=mysqli_fetch_array($records_dn)){
        echo '<option value="'.$row_dn['catagory'].'">'.$row_dn['catagory'].'</option>';
        }
        ?>
        </select>
        <br/>
        <br/>
        <div class="cf_header5">SIZE</div>
        <select name="size" style="width:150px; height:25px;font-size:16px">
        <option value="">Please select ....</option>
        <?php
        $sql_se = "SELECT distinct size FROM products where size !='' order by size";				
        $records_se=mysqli_query($dbcon,$sql_se);
        while ($row_se=mysqli_fetch_array($records_se)){
        echo '<option value="'.$row_se['size'].'">'.$row_se['size'].'</option>';
        }
        ?>
        </select>
        <br/>
        <br/>
        <input type="submit" name="submit" value="Search" />
        </form>
    <?php //get the value from dropdown list and stored it to a variable
    $material=isset($_POST['material']) ? $_POST['material'] : "";
    $size=isset($_POST['size']) ? $_POST['size'] : "";
    $description=isset($_POST['description']) ? $_POST['description'] : "";
    ?>
    </div>
    <div class="col-md-10">
    <div id="main">
        <header>
        <div class="logo-outer">
            <div class="logo"> <img src="../images/logo4.png" /> </div>
            <?php			
              //////SET UP THE TOTAL images PER PAGE & CALCULATIONS:
              $per_page = 12;// Number of images per page, change for a different number of images per page
              // Get the page and offset value:
              if (isset($_GET['page'])) {
              $page = $_GET['page'] - 1;
              $offset = $page * $per_page;
              }
              else {
              $page = 0;
              $offset = 0;
              } 
              //Display no of items in cart
              echo '<a href="viewcart.php?page=';echo $page+1;echo '&type=buckle';echo'">';
              echo "You have";
              $t_sql="select count(product_id) as nocart from cart where user_name='$user_name'";
              $t_run=mysqli_query($dbcon,$t_sql);
              $t_row=mysqli_fetch_array($t_run);
              $cart_count=$t_row['nocart'];
              echo '&nbsp';
              echo '<span class="badge" >';echo $cart_count; echo '</span>'; 
              echo ' items on list</a>';
              //end of display number of items in cart			
            ?>
            </div>
        </div>
        </header>
        <div class="margin">
            <div id="pagination"><!-- #pagination start -->
            <?php
            $images_sql="select count(id) from products where product_type='buckle'"; 
            if(isset($_POST['material'])and $material!='')
                {$images_sql=$images_sql." and material='$material'";}
            if(isset($_POST['description'])and $description!='')
                {$images_sql=$images_sql." and catagory='$description'";}
            if(isset($_POST['size'])and $size!='')
                {$images_sql=$images_sql." and size='$size'";}
            $images_sql=$images_sql." ORDER by id ASC";
            $result_images = mysqli_query($dbcon, $images_sql);
            $row_images = mysqli_fetch_array($result_images);								
            $total_images = $row_images[0];
            // Calculate the number of pages:
            if ($total_images > $per_page) {//If there is more than one page
            $pages_total = ceil($total_images / $per_page);
            $page_up = $page + 2;
            $page_down = $page;
            $display ='';//leave the display variable empty so it doesn't hide anything
            } 
            else {//Else if there is only one page
            $pages = 1;
            $pages_total = 1;
            $display = ' class="display-none"';//class to hide page count and buttons if only one page
            } 
            ////// DISPLAY THE PAGE COUNT AND BUTTONS:
            echo '<h2'.$display.'>Page '; echo $page + 1 .' of '.$pages_total;//Page out of total pages
          
            $i = 1;//Set the $i counting variable to 1
            echo '<div id="pageNav"'.$display.'>';// $display variable will do nothing if more than one page
            // Show the page buttons:
            if ($page) {
            echo '<a href="buckle.php"><button><<</button></a>';//Button for first page [<<]
            echo '<a href="buckle.php?page='.$page_down.'"><button><</button></a>';//Button for previous page [<]
            } 
            for ($i=1;$i<=$pages_total;$i++) {
            if(($i==$page+1)) {
            echo '<a href="buckle.php?page='.$i.'"><button class="active">'.$i.'</button></a>';//Button for active page, underlined using 'active' class
            }
            //In this next if statement, calculate how many buttonso show. 
            if(($i!=$page+1)&&($i<=$page+3)&&($i>=$page-1)) {//This is set for two below and two above the current page
            echo '<a href="buckle.php?page='.$i.'"><button>'.$i.'</button></a>'; }
            } 
            if (($page + 1) != $pages_total) {
            echo '<a href="buckle.php?page='.$page_up.'&size='.$size. '"><button>></button></a>';//Button for next page [>]
            echo '<a href="buckle.php?page='.$pages_total.'"><button>>></button></a>';//Button for last page [>>]
            }
            echo '</div>';// #pageNav end
            ?>
            </div>
            <div id="gallery"><!-- Gallery start-->
            <?php
            // DISPLAY THE images:
            //Define the SQL statement based on the dropdown list 
            if ($total_images>0)
            {	
            $img_sql="select * from products where product_type='buckle'"; 
            if(isset($_POST['material'])and $material!='')
            {$img_sql=$img_sql." and material='$material'";}
            if(isset($_POST['description'])and $description!='')
            {$img_sql=$img_sql." and catagory='$description'";}
            if(isset($_POST['size'])and $size!='')
            {$img_sql=$img_sql." and size='$size'";} 
            $img_sql=$img_sql." ORDER by id ASC LIMIT $offset, $per_page";
            $result = mysqli_query($dbcon, $img_sql);
            while($row = mysqli_fetch_array($result)) {//Open the while array loop
            //Define the image variable:
            $image=$row['images'];
            $product_id=$row['product_id'];
            $product_type=$row['product_type'];
            $size=$row['size'];
            $material=$row['material'];
            $description=$row['catagory'];
            echo '<div class="img-container">';
            echo '<div class="img">';
            echo '<img src="images/'.$image.'">';
            echo '</a>';
            echo '</div>';
            echo $product_id;echo '&nbsp &nbsp ';
            echo '<a href="addcart.php?page=';echo $page+1;echo '&action=add&id=';echo $product_id;echo '&size=';echo $size;echo '&type=';echo $product_type;echo '">Add to List</a>';
            echo '<br/>Size:';echo $size;
            echo '&nbsp &nbsp Material: '; echo $material ;
            echo '<br/>Description: ';echo $description;
            echo '</div>';// .img-container end
            }//Close the while array loop
            }
            else 
            {
            echo'<br/><br>';
            echo '<h1>Sorry! No result is matched, please search again.</h1>';
            echo'<br/><br>';										
            }	
            ?>
            </div>
        <!-- Gallery end-->
        <div class="clearfix"></div>
        </div>
    </div>
    <!-- #main end --> 
    </div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的逻辑似乎还可以,但是您的问题是,当您浏览低谷页面时,您会重新调用该文档,该文档不包含过滤器之类的变量,而是当前页面($_GET['page'])。

您也可以通过URL将过滤器作为变量发送,这不是进行表分页的最干净方法。

或者您将结果收集到javascript数组中,然后单击分页按钮以显示上一行/下一行。通过这样做,您还可以避免每次都刷新整个文档,这是不必要的。