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
----------------------
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
Most helpful comment
Hi @naghibi-farshad, try the following then, more simplified: