Core: InlineKeyboard Button One row in loop

Created on 13 May 2017  路  7Comments  路  Source: php-telegram-bot/core

Required Information

  • Operating system:
  • PHP version: 5.5
  • PHP Telegram Bot version :0.44.1
  • Using MySQL database: yes
  • MySQL version: 5.5
  • Update Method: Webhook
  • Self-signed certificate: yes

Hello friends
I can't create a InlineKey on row when get data frome database in loop:

My code examples:

foreach ($category as $key => $value) {
    $keyboardButton[] = new InlineKeyboardButton([
        'text' => $value['cat_name'],
        'callback_data' => $value['cat_id']
    ]);
}
$data = [
    'chat_id' => $chat_id,
    'caption' => 'Select a Category',
    'reply_markup' => new InlineKeyboard($keyboardButton)
];
Request::sendMessage($data);

Notic : output of each button in a row
Like:

Select a Category:
-----------------------
Category 1
-----------------------
Category 2
-----------------------
Category 3
----------------------

Most helpful comment

Hi @naghibi-farshad, try the following then, more simplified:

$inlineKeyboard = new InlineKeyboard([]);
foreach ($category as $key => $value) {
    $inlineKeyboard->addRow(new InlineKeyboardButton([
        'text'          => $value,
        'callback_data' => $key,
    ]));
}

$data = [
    'chat_id'      => $chat_id,
    'text'         => 'Select a Category',
    'reply_markup' => $inlineKeyboard,
];
return Request::sendMessage($data);

All 7 comments

try like this new InlineKeyboard(...$keyboardButton)

Also to debug check output of your Request::sendXXXX(), Telegram API usually tells whats wrong there.

@jacklul my problem is not solved. please advise, by sample code.
How is create InlineKeyboard syntax for creating buttons in a row by the loop is.

If you want them all in 1 row next to each other, try like this:

$keyboardButtons = [];
foreach ($category as $key => $value) {
    $keyboardButtons[] = new InlineKeyboardButton([
        'text'          => $value,
        'callback_data' => $key,
    ]);
}

$inlineKeyboard = new InlineKeyboard([]);
call_user_func_array([$inlineKeyboard, 'addRow'], $keyboardButtons);

$data = [
    'chat_id'      => $chat_id,
    'caption'      => 'Select a Category',
    'reply_markup' => $inlineKeyboard,
];
Request::sendMessage($data);

@jacklul Argument unpacking is PHP 5.6+.

@naghibi-farshad Did you manage to get it working?

hi @noplanman
the problem with taking a few buttons in a row.
the output should each button in a separate row.

Hi @naghibi-farshad, try the following then, more simplified:

$inlineKeyboard = new InlineKeyboard([]);
foreach ($category as $key => $value) {
    $inlineKeyboard->addRow(new InlineKeyboardButton([
        'text'          => $value,
        'callback_data' => $key,
    ]));
}

$data = [
    'chat_id'      => $chat_id,
    'text'         => 'Select a Category',
    'reply_markup' => $inlineKeyboard,
];
return Request::sendMessage($data);

tanks @noplanman
problem solved

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dorcu picture dorcu  路  3Comments

tchibomann picture tchibomann  路  3Comments

smaznet picture smaznet  路  4Comments

irmmr picture irmmr  路  3Comments

ttvd94 picture ttvd94  路  4Comments