2

对于我正在构建的 Telegram 机器人,我想根据返回的 PHP PDO 记录集Telegram API docs动态返回内联按钮。

硬编码一段好的函数代码如下所示。这是确认工作。它返回两行按钮。第一行包含两个按钮,第二行包含两个按钮。

$reply  = "Some message to show before the buttons";
$keyb   = array('inline_keyboard' => array(
            array(
                array('text'=>'Link text', 'callback_data'=>'/command'),
                array('text'=>"Link text", 'callback_data'=>'/command')
            ),
            array(
                array('text'=>'Link text', 'callback_data'=>'/command'),
                array('text'=>'Link text', 'callback_data'=>'/command')
            )
          )
        );

$replyMarkup = json_encode($keyb);
sendMessage($chatID, $reply, $replyMarkup);

到目前为止,一切都很好。但现在我想在给定 PHP 记录集的情况下动态填充这些按钮。

下面确实返回了所需的按钮,但我不知道如何在两个按钮之后指定一个截止点来创建第二行。在下面的格式中,所有按钮最终都在一行中。即使记录集返回 5 个结果。

$reply  = "Some message to show before the buttons";

$i=0;
// Loop through all results to create individual buttons
foreach ($stmt as $row) 
{
    $options[] = array('text'=>urlencode($row['title']), 'callback_data'=>'/x');
    $i++;
}

$keyb           = array('inline_keyboard' => array( $options ));
$replyMarkup    = json_encode($keyb);
sendMessage($chatID, $reply, $replyMarkup);

我考虑过使用带有模运算符($i%2=1)的 if 语句,但不知道如何处理定义行的父数组()...

...
if($i%2=1)
{ 
    $options[]="array("; // <-- Setting an array as a value will obviously fail
}    
... remaining code

很高兴听到任何可能对我有所帮助的想法!

谢谢。

4

2 回答 2

0

在这里原谅我,我必须跳进去大喊:

不!

到其他答案。

PHP 已经提供了一个可以将数组分解为“块”的函数,称为array_chunk()。不要使用递增和模数条件来生成密钥,请使用这一功能。

代码(演示):

$stmt = array(
            array('title'=>'Link ? text1', 'callback_data'=>'/x'),
            array('title'=>"Link & text2", 'callback_data'=>'/x'),
            array('title'=>'Link # text3', 'callback_data'=>'/x'),
            array('title'=>'Link * text4', 'callback_data'=>'/x'),
            array('title'=>'Link @ text5', 'callback_data'=>'/x')
      );

// prepare the subarrays using resultset
foreach($stmt as $row){
    $options[]=['text'=>urlencode($row['title']),'callback_data'=>'/x'];
}

// then chunk into pairs, assign key, json encode --> DONE
var_export(json_encode(['inline_keyboard'=>array_chunk($options,2)]));
//sendMessage($chatID,$reply,json_encode(['inline_keyboard'=>array_chunk($options,2)]));

输出:

{"inline_keyboard":[[[{"text":"Link+%3F+text1","callback_data":"\\/x"},{"text":"Link+%26+text2","callback_data":"\\/x"}],[{"text":"Link+%23+text3","callback_data":"\\/x"},{"text":"Link+%2A+text4","callback_data":"\\/x"}],[{"text":"Link+%40+text5","callback_data":"\\/x"}]]]}
于 2017-06-16T01:17:05.563 回答
0

这应该可以解决问题:

<?php
// DUMMY DATA
$stmt = array(
            array('title'=>'Link text1', 'callback_data'=>'/command'),
            array('title'=>"Link text2", 'callback_data'=>'/command'),
            array('title'=>'Link text3', 'callback_data'=>'/command'),
            array('title'=>'Link text4', 'callback_data'=>'/command'),
            array('title'=>'Link text5', 'callback_data'=>'/command')
      );

$i=0;
$r=1;
foreach ($stmt as $row)
{
    // here's the trick: $options[$r][]
    $options[$r][] = array('text'=>urlencode($row['title']), 'callback_data'=>'/x');
    $i++;
    if($i===2) { // if counter at row 3
        $r++; // increase row index here
        $i=0; // reset counter
    };
}

// for debugging only:
echo "<pre>";
var_dump($options);
echo "</pre>";
                                        // note the change here
$keyb           = array('inline_keyboard' => $options);
?>

@abfackeln 的版本(如果你懂德语的话,这是一个多么好的用户名..)更好,因为他使用模数并且不重置计数器。

于 2017-06-13T20:06:29.920 回答