Answer the question
In order to leave comments, you need to log in
Telegram, php how to write a pagination algorithm?
I have such a pagination algorithm for inline_keyboard that builds 2 columns of 6 elements, and if there are more than 12 but less than 24 elements (I didn’t have more than 24 before), I manually checked this and duplicated the code:
while ($row = mysqli_fetch_assoc($result)) {
$one_row[$cnt] = [
"text" => $row["test"],
"callback_data" => "test"
];
$cnt++;
if ($cnt == 2) {
$buttons[] = $one_row;
$one_row = [];
$cnt = 0;
}
}
Answer the question
In order to leave comments, you need to log in
You add data to buttons in an iteration when the number of elements in $one_row == your number of columns (in the particular case 2).
Accordingly, if you have a non-multiple number of data, then there is a stub.
The obvious solution is to check what is in $one_row after the loop and save if necessary.
$columns = 2;
$buttons = [];
$one_row = [];
while ($row = mysqli_fetch_assoc($result)) {
$one_row[] = [
"text" => $row["test"],
"callback_data" => "test"
];
if (count($one_row) == $columns) {
$buttons[] = $one_row;
$one_row = [];
}
}
if (count($one_row) > 0) {
$buttons[] = $one_row;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question