Re: Looping through array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ashley M. Kirchner wrote:

Say I have an array containing ten items, and I want to display them in a table as follows:

    5 4 3 2 1
   10 9 8 7 6

What's the best way to loop through that array to do that? My thinking gets me to create a loop for 5 through 1, repeated twice, and the second time I add '5' to the index value. There's got to be a saner way...

Something like this perhaps...

$arr = array(...);
$per_row = 5;
$elem = count($arr);
for($i=0; $i<$elem; $i++) {
   if( $i == 0 )
      echo "<tr>";
   else if( $i % $per_row == 0 )
      echo "</tr><tr>";
   echo "<td>$arr[$i]</td>";
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux