I would like to display some products from database and use pagination as well as checkboxes for filtering. My pagination is working fine. And when I click checkboxes and press "submit," I do get filtered results on the first page. However, when I move to the second or any other page, the checkboxes automatically become unchecked and the filtering gets lost. Here is my HTML code:
<form action="" method="get">
<input type="checkbox" name="brand[]" value="iPhone">iPhone<br>
<input type="checkbox" name="brand[]" value="iPad">iPad<br>
<input type="checkbox" name="brand[]" value="Samsung">Samsung<br>
<input type="checkbox" name="brand[]" value="Huawei">Huawei<br>
<input type="submit" name="submit" value="Submit">
</form>
<?php include 'display_products.php';?>
In order to apply filtering I use an IF statement:
if (isset($_GET['brand'])) {
$filter = implode('","',$_GET['brand']);
$sql='SELECT * FROM products WHERE brand IN ("' . $filter . '") LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) {
echo '<div><h3>' . $row['title'] . '</h3><img src="' . $row['image'] . '"<h4>' . $row['price']. '</h4></div>';
}
} //else display all products from the table of the database
I assume that when I go to the next page, my checkboxes get unchecked, this $_GET['brand'] becomes empty and the "else" statement is activated. I tried to find solutions for this problem, but some of them were not effective and some were too hard for me to understand (I am a beginner). Could you please explain in simple terms how to keep the checkboxes checked and how to keep the filtering throughout all the pages? I saw such ideas as "use session" or "keep the data in url," but I can't figure out how to implement it. So if you are more specific, I would be super grateful. Thank you very much!
Aucun commentaire:
Enregistrer un commentaire