Author Topic: How to pagebreak after displaying each 10 keys from an array?  (Read 96 times)

phpn00b

  • New Member
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
How to pagebreak after displaying each 10 keys from an array?
« on: February 03, 2012, 05:06:44 PM »
I have an array with 100 keys, and I want to echo all the keys, however, I want to add a <br /> after displaying every 10th key: keys 1-10 (0-9), 11-20,  21-30...so on.

Smokey PHP

  • Web Developer
  • Expert PHP Helper
  • Senior Member
  • *****
  • Posts: 529
  • Karma: +5/-0
    • View Profile
Re: How to pagebreak after displaying each 10 keys from an array?
« Reply #1 on: February 04, 2012, 09:13:00 AM »
PHP Code: [Select]
//if the keys of your array are default
foreach($array as $key => $value)
{
    echo 
$value;
    if(
$key%10 == 0) echo '<br />';
}

//if the array uses custom keys
$i 0;
foreach(
$array as $value)
{
    echo 
$value;
    
$i++;
    if(
$i%10 == 0) echo '<br />';
}


You might need to swap the placement of the line break echo with the $value echo if it's breaking on the wrong number, but those bits of code should help you on your way.