Re: What kind of looping ?

From: Date: Tue, 27 Nov 2001 21:24:06 +0000
Subject: Re: What kind of looping ?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-75813@lists.php.net to get a copy of this message
"Steve Maroney" <steve@stevenet.dhs.org> wrote: > Now $file_list[] has some number a file names. I want print 4 file names > into a row. Each file name has to be in its on cell. > > <table> > <tr> <td>FILE 0</td> <td>FILE 1</td> <td>FILE > 2</td> <td>FILE 3</td> </tr> > <tr> <td>FILE 4</td> <td>FILE 5</td> <td>FILE > 6</td> <td>FILE 7</td> </tr> > </table> > > I would image I would use a for loop, but I cant figure it out. > Can somone assist me ? The % (modulus) operator is the key to this type of looping. I didn't test the code below, but I've done this enough times that I'm pretty sure it's correct. If not it should be pretty easy to correct after you test it. $columns = 4; $array_count = count( $file_list ); for ( $i = 0; $i < $array_count; $i++ ) { if ( $i % $columns == 0 ) { $o .= '<tr>'; } $o .= "<td>{$file_list[$i]}</td>"; if ( $i % $columns == $columns - 1 ) { $o .= '</tr>'; } } // Create blank table cells if final row doesn't use all columns. // You can get away without this on broken browsers like IE, but // then you'll generate invalid HTML and browsers like NS // won't display your pages. if ( $array_count % $columns > 0 ) { $o .= str_repeat( "<td>&nbsp;</td>", $columns - ( $array_count % $columns ) ) . "</tr>"; } -- Steve Werby President, Befriend Internet Services LLC http://www.befriend.com/

« previous php.general (#75813) next »