问题
hi I have some product cat in my database I want to generate inline keyboard from the theme but I want to like this
we have
cat1-ca2-ca3-ca4-ca5
I want to like this
cat1 cat2
cat3 cat4
cat5
I need the loop for this result thanks
回答1:
When you get the values that you want to show in the keyboard, you need to store these in array. This example was did in Javascript, you must found the equivalent in php code.
If you only have 5 products you don't need a loop, just:
...
{
'reply_markup': JSON.stringify({
keyboard:
[
[{'text': 'cat1'},{'text': 'cat2'}],
[{'text': 'cat3'},{'text': 'cat4'}],
[{'text': 'cat5'}]
],
one_time_keyboard: true,
resize_keyboard: true
})
}
...
If you have undefined products elements you must use a for loop:
var keyboard = [];
var products = ['cat1', 'cat2', 'cat3', 'cat4', 'cat5'];
for (var i = 0; i < products.length; i++) {
keyboard.push([{'text': products[i]}]);
}
...
{
'reply_markup': JSON.stringify({
inline_keyboard: keyboard
})
}
来源:https://stackoverflow.com/questions/44697686/generate-inline-keyboard-telegram-bot-from-the-database