A
A
Andrey Boychenko2020-08-07 22:02:14
MySQL
Andrey Boychenko, 2020-08-07 22:02:14

Find pagination page by order ID, error?

Good evening, I have a page with pagination, 50 posts per page. In total, there are more than 2k records. In some situations, I need to know on which page, which record is located. In general, I thought that nothing complicated and wrote the following code:

$id = request()->onote;
$per_page = 50;
$total_page = $this->has('removals')
                ->wherehas('removals.product')
                ->where('archived', 0)
                ->paginate($per_page)
                ->lastPage();

            $index = $this->has('removals')
                ->wherehas('removals.product')
                ->where('archived', 0)
                ->where('id', '<=', $id)
                ->count();

            $page = round($index/$per_page);
            $page = ($total_page-$page);


I have information about specific orders on a completely different page and I want to go to the page with the order on click. The script partially works as it should, here is a specific example, I have 2 orders on page 4 and 1 order on page 8. The script correctly determines the page for orders from page 4, but for an order from page 8 it says that the order is on page 7 , although it is in fact on page 8.
Tell me, where exactly did I make a mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Boychenko, 2020-08-07
@Ka4_Piton

I really didn’t want to, but I had to do everything with the help of recursion, since the problem needed to be solved as quickly as possible.

V
vism, 2020-08-08
@vism

because "school knowledge is not useful in life" class 6 probably
Hint

$page = round($index/$per_page);
$page = ($total_page-$page);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question