0

I'm trying create a inline_keyboard with number between 0 and 99. But the loop (a for structure) only shows from 0 to 7.

My code es:

$data = http_build_query([
        'text' => 'Selecciona un numero:',
        'chat_id' => $update['message']['from']['id']
]);

$keyboard = array();

for($i=0;$i<100;$i++) {
    array_push($keyboard,array("text" => $i,"callback_data" => $i));
}

$resultado = json_encode(['inline_keyboard'=>array($keyboard)]);

// Send keyboard
file_get_contents($botAPI . "/sendMessage?".$data."&reply_markup=".$resultado);

And this is a screenshot from my smartphone:

Screenshot

Why does the loop only count to seven?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
monicapo
  • 111
  • 1
  • 12
  • Try changing the loop to `for($i=1;$i<100;$i++) {`. Does it only show 1 through 8 then? – Chris Haas Sep 08 '20 at 17:10
  • Yes. If I change $i value, the loop works fine, but only it shows eight numbers. Example: 0-7, 1-8 or 12-19. – monicapo Sep 08 '20 at 17:55
  • Okay, I'm very certain that the thing you are putting this into (which I have no idea what it is) is only allowing or showing 8 items. Your loop is fine, the thing holding it has a problem, and you'll need to diagnose that. If this were a webpage I'd say it has an "overflow: hidden" on it or similar with a max length of 65% of the screen. – Chris Haas Sep 08 '20 at 18:01
  • The result shows in telegram bot, not in a page. If I copy the code in a php website it works fine and shows all values. – monicapo Sep 08 '20 at 18:05
  • Yes, my point is that your code is sound, it is a limitation that Telegram imposes, probably because 100 buttons is impracticable for someone to use. I can't find any actual documentation to the limit however, and I'd be curious if you rotated the device would more show. This point does say you can pass an array of arrays to get multiple rows, but you'd have to try it to be sure. https://stackoverflow.com/a/50075959/231316 – Chris Haas Sep 08 '20 at 20:08
  • Have you tried to make numbers in multiple rows? – wowkin2 Sep 09 '20 at 09:29

1 Answers1

0

Ok. Telegram limits eight buttons each row. Then, we have add rows in array each eight buttons. My working code is:

    $keyboard = array();
    $linea = array();

    for($j=0;$j<100;$j++) {
        array_push($linea,array("text" => $j,"callback_data" => $j));
        $contadorFilas++;       
        if($contadorFilas==6) {
            array_push($keyboard,$linea);           
            $linea = array();           
            $contadorFilas = 0;
        }
    }
        
    array_push($keyboard,$linea);           
    $resultado = json_encode(['inline_keyboard'=>$keyboard]);
monicapo
  • 111
  • 1
  • 12