从数据库生成嵌入式键盘电报机器人
问题描述:
我的数据库中有一些产品猫,我想从主题生成嵌入式键盘,但我想要这样
hi I have some product cat in my database I want to generate inline keyboard from the theme but I want to like this
我们有
cat1-ca2-ca3-ca4-ca5
我想要这样
cat1 cat2
cat3 cat4
cat5
我需要循环才能获得此结果
I need the loop for this result thanks
答
获得要在键盘上显示的值时,需要将它们存储在数组中.这个示例是用Javascript完成的,您必须在php代码中找到等效的内容.
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.
如果您只有5种产品,则不需要循环,只需:
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
})
}
...
如果您有未定义的product元素,则必须使用for循环:
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
})
}