Re: Nestled 'while's or 'for's or 'foreach's -- I'm lost

From: Date: Sun, 17 Aug 2003 23:24:49 +0000
Subject: Re: Nestled 'while's or 'for's or 'foreach's -- I'm lost
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-159870@lists.php.net to get a copy of this message
* Thus wrote Verdon vaillancourt (verdon@lacmedia.com): > Hi, somewhat newbie warning :) > > I'm trying to take a paged result set and divide it into two chunks for > displaying on the page. Basically making something that looks like a typical > thumbnail gallery. I'm limiting my result set to 6 records and want to > display it as 2 rows of 3 cells with a record in each cell. I've had no > trouble getting my result set, paging etc. I'm not having much luck This can easily be done using the mod operator (%): $cols_per_page = 3; $cols_per_page_break = $cols_per_page - 1; // just to speed the loop echo "<table>"; echo "<tr><th>Col Headings....</th></tr>"; $counter = 0; while($row = db_fetch_row() ) { $new_row = $counter++ % $cols_per_page; if ($new_row == 0 ) { echo "<tr>"; // open a row } echo "<td>Col Data</td>"; if ($new_row == $cols_per_page_break ) { // close and open a row print "</tr>"; } } // Make sure the row was closed in case of uneven recordset. if ( ( ($counter - 1) % $cols_per_page) != $cols_per_page_break ) { // close and open a row // cause we didn't close it in the loop print "</tr>"; } echo "</table>"; //close row and table um.. ok, so it wasnt that easy.. I made this a little more complicated than I was expecting it to be. btw, this is completely untested. Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#159870) next »