PHP Mysql - create previous and next link from current customer -


i have page listing customers:

/customers/ 

the order of customers on page not sequential:

1st: id 111 2nd: id 567 3rd: id 345 etc 

that's because sql query lets user select customer order based on first name, last name, or nickname.

"select customer_id customers status = 1 order " . $customer_order 

so when on 2nd customer in example above:

/customer/567/ 

so problem want display previous , next button this, can't figure out how:

<a href="111">prev</a> <a href="567">current</a> <a href="345">next</a> 

i managed previous link working in while loop it's ugly. tried using current(), prev(), , next() failed.

most solutions i've seen on stackoverflow sequential ids either -1 or 1 current id, or "limit 1" in query i'm sure doesn't work in situation after testing this.

any ideas?

you can assign row number query , use row number identify previous , next records:

select (@row_number := @row_number + 1) row_number, customer_id  customers, (select @row_number := 0) rn status = 1  order firstname 

if have row number current customer, find next/previous records using main query sub-query:

select customer_id next_id  (     select (@row_number := @row_number + 1) row_number, customer_id      customers, (select @row_number := 0) rn     status = 1      order firstname ) ordered_customers ordered_customers.row_number = :current_row_number + 1 

if have current id:

select customer_id next_id  (     select (@row_number := @row_number + 1) row_number, customer_id      customers, (select @row_number := 0) rn     status = 1      order firstname ) ordered_customers ordered_customers.row_number = 1 + (      select row_number           (         select (@row_number2 := @row_number2 + 1) row_number, customer_id          customers, (select @row_number2 := 0) rn         status = 1          order firstname     ) ordered_customers2     ordered_customers2.id = :current_id )  

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -