E
E
Evgeny2022-01-27 02:39:08
PHP
Evgeny, 2022-01-27 02:39:08

Why does the bot give out an extra button on the dynamic keyboard?

The telegram bot has a dynamic keyboard, the number of buttons of which depends on the rows in the database, the text in these buttons is ordinary characters.

The problem is that if I try to add additional characters, the bot will give 1 more button, the text of which will be these added characters

Example:

Keyboard code without adding characters

$rows = $array_count;
        $columns = 1;
        $keyboard = [];
        for ($i = 0; $i <= $rows; $i++) {
            $rowKeys = [];
            for ($j = 1; $j <= $columns; $j++) {
                $token_get = secret_token_get($user_id)[$i];
                $token_symbols = substr($token_get['token'], -5);
                $rowKeys[] = ['text' => $token_symbols, 'callback_data' => 'token-' . $i]; // Нумерация токенов
            }
            $keyboard[] = $rowKeys;
        }


Bot response

61f1db4d7e4ef968311516.png

Code with asterisks added

$rows = $array_count;
        $columns = 1;
        $keyboard = [];
        for ($i = 0; $i <= $rows; $i++) {
            $rowKeys = [];
            for ($j = 1; $j <= $columns; $j++) {
                $token_get = secret_token_get($user_id)[$i];
                $token_symbols = substr($token_get['token'], -5);
                $rowKeys[] = ['text' => '***' . $token_symbols, 'callback_data' => 'token-' . $i]; // Нумерация токенов
            }
            $keyboard[] = $rowKeys;
        }


Bot response with an extra button

61f1db8d5b00b033103521.png

What could be the reason and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Gordinskiy, 2022-01-27
@fakin_kiska

You are iterating from zero up to and including the number of elements in the array. And it is necessary - not inclusive.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question