Answer the question
In order to leave comments, you need to log in
How to design the "Next Product"/"Previous Product" button?
Hello!
I would like to ask how you can design a button, by clicking on which it will show the next product from the database - that is, I press the button and the product carousel moves one product back / forward.
I use aiogram
@dp.callback_query_handler(text= 'ххх')
async def name(call: types.CallbackQuery):
kb = InlineKeyboardMarkup(row_width=2)
cur=con.cursor()
cur.execute('SELECT Name, Price FROM ххх')
row=cur.fetchall()
print(row)
next =InlineKeyboardButton(text= 'Следующий',callback_data= 'next')
previous =InlineKeyboardButton(text= 'Предыдущий',callback_data= 'previous')
add = InlineKeyboardButton(text= 'Добавить в корзину',callback_data= 'add')
kb.add(next,previous,add)
await call.message.answer(text= row,reply_markup=kb)
@dp.callback_query_handler(text= 'next')
async def next_button (call: types.CallbackQuery):
#cur=con.cursor()
#cur.execute('SELECT Name, Price FROM xxx WHERE Id += 1')
#row=cur.fetchall()
#print(row)
Answer the question
In order to leave comments, you need to log in
You either need to remember the position of the current user in the product carousel in order to correctly choose which one will be the previous / next / current one to add (as already written above), or use the data of specific products as callback_data buttons.
For example, something like this (items is a dictionary of goods with articles as keys):
#
numbers = items.keys()
index_current = numbers.index(number_current)
if index_current > 0:
number_prev = numbers[index_current-1]
else:
number_prev = numbers[-1]
if index_current < len(numbers)-1:
number_next = numbers[index_current+1]
else:
number_next = numbers[0]
next =InlineKeyboardButton(text= 'Следующий',callback_data= f'item:{number_next}')
previous =InlineKeyboardButton(text= 'Предыдущий',callback_data= f'item:{number_prev}')
add = InlineKeyboardButton(text= 'Добавить в корзину',callback_data= f'add:{number_current}')
...
if call.data.startswith('item:'):
number_current = call.data[5:]
show_this_item_to_user
if call.data.startswith('add:'):
number_current = call.data[4:]
add_this_item_to_basket
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question