Re: gallery (4 in a row) // carefull newbie

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

 



Mike Johnson wrote:

From: user@xxxxxxxxxxxxxx [mailto:user@xxxxxxxxxxxxxx]



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!



Heh, that looks better than what I usually do, but to put a clean finish on it, you could do something like:

$loopcount = $count % 4;
if($loopcount > 0)
{
   for(;$loopcount < 4;++$loopcount) echo "<td>empty cell</td>\n";
   echo "</tr>\n";
}

That should work, and could be cleaned up a bit better I imagine...

Chris

--
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