Недавно я пишу каталог продукции для моей компании. Я использую раскрывающийся список, чтобы предоставить фильтр для списка продуктов, представленных в виде сетки, с разбиением на страницы. Фильтр работает нормально, если товары не более 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 ' ';
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 '    ';
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 '    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>





Ваша логика выглядит нормально, но ваша проблема заключается в том, что когда вы перемещаетесь по страницам, вы делаете новый вызов документа, который включает не такие переменные, как фильтры, а текущую страницу ($_GET['page']).
Либо вы отправляете фильтры как переменные через URL-адрес, что не является самым чистым способом разбиения таблицы на страницы.
Или вы собираете результаты в массив javascript, а затем при нажатии на кнопку разбиения на страницы отображаете предыдущие/следующие строки. Делая это, вы также избегаете обновления всего документа каждый раз, что не является необходимым.
Большое тебе спасибо! Поскольку проект срочный, я изменил рабочий процесс программы, и вопрос стал недействительным.