Advertisement
Hitmare

Inlinekeyboard , two column keyboard with odd amount of buttons

Oct 19th, 2023
1,415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.77 KB | None | 0 0
  1.         // $safetensors is my array with my values. code can be adapted for database useage.
  2.         $switch = TRUE; // switch variable for even/odd value
  3.         $temparray = array(); //temporary array for building side by side keyboard row
  4.         $numItems = count($safetensors) - 1; //count how many elements there are
  5.         $inline_keyboard = new InlineKeyboard([]); //array for the inlinekeyboard itself
  6.         $elements = 1; // this is optional. tracking how many buttons there are already to keep keyboard short. for menu system
  7.         foreach ($safetensors as $key => $lora) {
  8.            
  9.             if ($switch) { //checking if $switch is true. if yes, first (left) button is created and put into the $temparray.
  10.                 $temparray[0] = ['text' => $lora, 'callback_data' => "lora-" . $lora];
  11.                 $switch = FALSE; //setting $switch to false for next iteration
  12.             }
  13.             else { //if Switch is false, 2nd (right) button is created and put into $temparray
  14.                 $temparray[1] = ['text' => $lora, 'callback_data' => "lora-" . $lora];
  15.                 $switch = TRUE;
  16.                 $inline_keyboard->addRow($temparray[0], $temparray[1]); //adding both buttons into $inline_keyboard
  17.                 $temparray = array(); //clearing $temparray for next buttons
  18.             }
  19.             if ($key == $numItems and count($temparray) > 0) { //check if last item is reached and if $temparray has still items left. If an odd amount of emelents are in the array, only one item is left in $temparray.
  20.                 $inline_keyboard->addRow($temparray[0]); // adding last single button to the inline keyboard
  21.             }
  22.             $elements++; //optional , increment $elements by one
  23.             if ($elements == 50) { break; }; //if $elements limit is reach, stop foreach loop
  24.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement