H
H
Httdientee2021-12-26 21:35:20
PHP
Httdientee, 2021-12-26 21:35:20

Buttons are not displayed in a row php?

array_push($list['inline_keyboard'], array(array('text' => $cat['name'], 'callback_data' => $cat['name'])));


Now it's like this
61c8b5cee10ef897859643.png

. And you need to line up, what's wrong here? It seems that the data is in one array, and in fact they should be displayed in a row

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ambal245, 2021-12-31
@ambal245

$url = "https://qna.habr.com/q/1093980";
$buttons[] = $this->buildInlineKeyBoardButton('Кнопка 1', '1');
$buttons[] = $this->buildInlineKeyBoardButton('Кнопка 2', '2');
$buttons[] = $this->buildInlineKeyBoardButton('Кнопка 3', '3');
$buttons[] = $this->buildInlineKeyBoardButton('Кнопка 4', '', $url );
$buttons[] = $this->buildInlineKeyBoardButton('Кнопка 5', '5');
// разбиваем кнопки по рядам
array_chunk($buttons, 3); // 3 кнопки в ряду

public function buildInlineKeyboardButton($text, $callback_data = '', $url = '')  {
        // рисуем кнопке текст
        $replyMarkup = [  'text' => $text,  ];
        // пишем одно из обязательных дополнений кнопке
        if ($url != '') { 
             $replyMarkup['url'] = $url;
        } elseif ($callback_data != '') {
             $replyMarkup['callback_data'] = $callback_data;
        }
        // возвращаем кнопку
        return $replyMarkup;
    }
    public function buildInlineKeyBoard(array $options)  {
        // собираем кнопки
        $replyMarkup = [
            'inline_keyboard' => $options,  ];
        // преобразуем в JSON объект
        $encodedMarkup = json_encode($replyMarkup, true);
        // возвращаем клавиатуру
        return $encodedMarkup;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question