Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // $safetensors is my array with my values. code can be adapted for database useage.
- $switch = TRUE; // switch variable for even/odd value
- $temparray = array(); //temporary array for building side by side keyboard row
- $numItems = count($safetensors) - 1; //count how many elements there are
- $inline_keyboard = new InlineKeyboard([]); //array for the inlinekeyboard itself
- $elements = 1; // this is optional. tracking how many buttons there are already to keep keyboard short. for menu system
- foreach ($safetensors as $key => $lora) {
- if ($switch) { //checking if $switch is true. if yes, first (left) button is created and put into the $temparray.
- $temparray[0] = ['text' => $lora, 'callback_data' => "lora-" . $lora];
- $switch = FALSE; //setting $switch to false for next iteration
- }
- else { //if Switch is false, 2nd (right) button is created and put into $temparray
- $temparray[1] = ['text' => $lora, 'callback_data' => "lora-" . $lora];
- $switch = TRUE;
- $inline_keyboard->addRow($temparray[0], $temparray[1]); //adding both buttons into $inline_keyboard
- $temparray = array(); //clearing $temparray for next buttons
- }
- 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.
- $inline_keyboard->addRow($temparray[0]); // adding last single button to the inline keyboard
- }
- $elements++; //optional , increment $elements by one
- if ($elements == 50) { break; }; //if $elements limit is reach, stop foreach loop
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement