RE: [PHP] gallery (4 in a row) // carefull newbie

From: Date: Tue, 28 Dec 2004 19:07:13 +0000
Subject: RE: [PHP] gallery (4 in a row) // carefull newbie
Groups: php.general 
Request: Send a blank email to php-general+get-205263@lists.php.net to get a copy of this message
From: user@domain.invalid [mailto:user@domain.invalid] > Hi all > > I am making a gallery for my Homepage. Now, I want to make > little tumbnails of my work and make a table out of them. > So, to not put them all in a row, what whould make the site > unreadable, i have to put them in a little table, an i think > 4 in each row would be ok. > > I like to read out the pic's name etc. out of a mysql table. > But how do make the script to write a </tr> after it read > out 4 entries of a mysql table? > > Thanks in advance :) What I generally do is use the mod (%) operator to keep track of where it is. It returns the remainder of the divisional operator, as such: 0 % 4 == 0 1 % 4 == 1 2 % 4 == 2 3 % 4 == 3 4 % 4 == 0 5 % 4 == 1 6 % 4 == 2 etc... That way, you have a guaranteed 4-member looping variable. Try something like this: <? $result = mysql_query("SELECT * FROM foo ORDER BY bar"); $count = 0; while ($row = mysql_fetch_assoc($result)) { $loopcount = $count % 4; if ($loopcount == 0) { echo "<tr>\n"; } echo "<td>{$row['baz']}</td>\n"; if ($loopcount == 3) { echo "</tr>\n"; } } ?> The only problem is that at the end of the while() loop, if you want to have perfectly-formatted HTML, you need to figure out if it left off in the middle of a row and fill with N empty <td></td> tags. Also, if there's a better way to do this, I'm all ears. This is the way I've done it for years, but it /is/ a pain to do. Anyway, HTH! -- Mike Johnson Smarter Living, Inc. Web Developer www.smarterliving.com mike@smarterliving.com (617) 886-5539

« previous php.general (#205263) next »