I have an inline button pagination and a database. text[0], callbackdata1 I need them to be output five pieces(and so until the database runs out, it can have at least 100 elements) in turn, but I just have the first element repeated five times,then also the second, third, and so on
cur.execute("SELECT * FROM data")
data = cur.fetchall()
def paginator(page: int=0):
builder = InlineKeyboardBuilder()
for i in range(5):
button = InlineKeyboardButton(text=data[page][0], callback_data=data[page][1])
builder.add(button)
builder.adjust(1)
builder.row(
InlineKeyboardButton(text="⬅", callback_data=kb.Pagination(action="prev", page=page).pack()),
InlineKeyboardButton(text="➡", callback_data=kb.Pagination(action="next", page=page).pack()),
width=2
)
return builder.as_markup()
@dp.callback_query(kb.Pagination.filter(F.action.in_(["prev", "next"])))
async def pagination_handler(call: CallbackQuery, callback_data: kb.Pagination):
page_num = int(callback_data.page)
page = page_num - 1 if page_num > 0 else 0
if callback_data.action == "next":
page = page_num + 1 if page_num < (len(data) - 1) else page_num
with suppress(TelegramBadRequest):
await call.message.edit_text(
f"list",
reply_markup=paginator(page)
)
await call.answer()
@dp.message()
async def echo(message: Message):
msg = message.text.lower()
if msg == "list":
await message.answer("lis", reply_markup=paginator(0))
As I understand both of our solutions are based on the same code, so that's how I did that, it may be helpful:
And to display the first 5 records from your query you should do something like that: