S
S
Sergey Miller2021-06-25 14:21:03
PHP
Sergey Miller, 2021-06-25 14:21:03

Why does pagination work incorrectly when filtering?

Hello, I'm looking for advice and help.

I have a page where I retrieve information from a database.
And since there is a lot of information, you need to make pagination. In order not to write a bicycle, I found a pagination class on a good site. here

and use it. There was a problem:

When loading the page, everything works fine:

// пагинация
include($dr.'/pagination/pagination.php');
$peger = new DBPaginator($url2, 6);  // 6 - колво выводов на странице

$items = $peger->getItems("SELECT * FROM `apple` ORDER BY `id` DESC");

foreach ($items as $item){
...
}


So pagination works great. BUT.

I have filters.

Suppose I display all the apples on a page from the database. The paginator on the page displays 6 apples and the bottom navigation itself is

1,2,3,4,5....

If I select a filter (for example, red apples), at this time Ajax sends a request to php with the specified filter value ( red , For example). Php takes a filter value and makes a new fetch from the database
// пагинация
include($dr.'/pagination/pagination.php');
$peger = new DBPaginator($urlForPagi, 6);  // 6 - колво выводов на странице

$items = $peger->getItems("SELECT * FROM `apple ` WHERE `color` = $_POST['color'] ORDER BY id DESC");


and through echo json_encode returns the result, and jquery loads new apples onto the page, deleting the previous ones. No page reload. A new pagination is also loaded (pagination of a new selection ( red apples )).

I don't understand the following. When displaying all the apples on the page, there is pagination, for example, 1,2,3,4,5, and when red apples are selected, the old pagination is deleted and a new one is loaded from the selection 1.2 (6 pieces of red apples per page) - counted red apples in the database and realized that pagination works correctly in this sense. BUT.

When only red apples from the database are loaded and pagination of red apples appears, it is worth clicking, for example, on page 2, then for some reason all apples are displayed, but only red ones should.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2021-06-25
@ThunderCat

one)

WHERE `color` = $_POST['color']
In general, a hole, at least some kind of check, I am silent about prepared expressions that are a must-have like that ...
2) the post method is not very suitable for passing filter and page parameters, for this they usually use get parameters that are easy to integrate into links .

V
Vasya Bulka, 2021-06-25
@Anonymous24

I'm not a PHP person, but as a variant .
I can assume that it is necessary to add code to the page so that the request is saved in the link)))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question